Dynamic Refresh after the ListView content changes
Basic Points of knowledge:
1. Update the adapter adapter data source
2. Calling the adapter adapter Refresh Method Notifydatasetchanged ()
You first need to define the ListView and the corresponding adapter adapter:
//ListView for displaying the list of usersPrivateListView userlist =NULL;//user list userlist the corresponding adapterSimpleadapter Simpleadapter =NULL;//user list userlist data source for the corresponding adapterList<map<string,?>> Datasimpleadapter =NewArraylist<map<string,?>>();//User list ListView row layout elementint[] Userlistviewto =NULL;//user list the adapter key value corresponding to the ListView row layout elementstring[] Userlistviewfrom =NULL; /** * Initialize the user list component adapter. */ Public voidInituserlistviewadapter () {userlist=(ListView) Findviewbyid (r.id.user_list_users); Userlistviewto=New int[]{r.id.user_list_item_user_id, r.id.user_list_item_user_login_id, R.id.user_list_item_u Ser_login_pass}; intLen =userlistviewto.length; Userlistviewfrom=NewString[len]; for(intI=0; i<len;i++) {Userlistviewfrom[i]=string.valueof (i); } }
Then configure the ListView adapter Adapter:
/** * Displays the ListView component. */ Public voidShowlistview () {//user list listview layout file intResource =R.layout.user_list_item; //preparing the adapter data sourceCreateadapterdata (); //binds the adapter and returnsSimpleadapter =Newsimpleadapter (context, Datasimpleadapter, resource, Userlistviewfrom, USERLISTVIEWTO); //sets the adapter for the ListView component.Userlist.setadapter (Simpleadapter); } /** * Generate adapter Data source * @param itemCount*/ Public voidCreateadapterdata () {//preparing the adapter contentsDatasimpleadapter.clear ();//ensure that the data source is empty intItemlength =3; for(intI=0; i<itemlength;i++) {Map<String,Object> map =NewHashmap<string,object>(); Map.put (userlistviewfrom[0],"data column 1-"+i); Map.put (userlistviewfrom[1],"data column 2-"+i); Map.put (userlistviewfrom[2],"data column 3-"+i); Datasimpleadapter.add (map); } }
Finally, just update the data source Datasimpleadapter for the adapter simpleadapter, and then execute the method:
// refreshes the ListViewsimpleadapter.notifydatasetchanged ();
Dynamic Refresh after the Android listview content Changes