Dynamic Refresh after the Android listview content Changes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.