Android -- Listview does not use the yydatasetchanged method to update data. androidlistview updates data.

Source: Internet
Author: User

Android -- Listview does not use the yydatasetchanged method to update data. androidlistview updates data.

I. Introduction 
The following describes how to update data in listview:
1. listview. setadapter () is called each time data is updated (); 
2. Call adapter. notifydatasetchanged () each time data is updated (); 
3. Add the update function in the Custom adapter;

Blog Author: It 1zhai male
Reprinted please indicate address: http://blog.csdn.net/u013293125/article/details/52858396

Here, we will introduce one by one, by the way, the working principle and mechanism of ListView do not understand can look at this article: http://blog.csdn.net/guolin_blog/article/details/44996879 (the great god is to see the original code, here I offer my knees ).
1. listview. setadapter () is called each time data is updated (); 
This method is the least efficient, because it refresh all the data no matter whether you need to refresh other data, that is to say, refresh the entire listview, it is estimated that Android users will not use this method, but we still list it and can compare it with other methods.

1.1 first:

Click Update:

1.2 activity_main.xml File

<LinearLayout android: orientation = "vertical" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/container" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = "com. example. listviewupdate. mainActivity "tools: ignore =" MergeRootFrame "xmlns: android =" http://schemas.android.com/apk/res/android "> <Button android: id =" @ + id/btn "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: text = "Update"/> <ListView android: id = "@ + id/listview" android: layout_width = "match_parent" android: layout_height = "wrap_content"> </ListView> </LinearLayout>

1.3 item. xml file:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView         android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <TextView         android:id="@+id/tv2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>

1.4 MainActivity. java file:

Package com. example. listviewupdate; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. support. v7.app. actionBarActivity; import android. support. v7.app. actionBar; import android. support. v4.app. fragment; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. button; import android. widget. listView; import android. widget. textView; import android. OS. build; public class MainActivity extends Activity {private ListView listview; private List <Map <String, Object> list = new ArrayList <Map <String, Object> (); private MyAdapter adapter; private Button btn; Map <String, Object> map; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listview = (ListView) findViewById (R. id. listview); // initialization data for (int I = 0; I <8; I ++) {map = new HashMap <String, Object> (); map. put ("Id", "100" + I); map. put ("Name", "Name _" + I); list. add (map);} adapter = new MyAdapter (this, list); listview. setAdapter (adapter); btn = (Button) findViewById (R. id. btn); // For example, to update the Name of the third row in listview, but the following method is to re-load the adapter // That is to say, it refreshes the entire listview, no matter whether other data needs to be updated or not; btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub map = list. get (2); map. put ("Name", "updated Name"); // The first parameter of MyAdapter does not need this because it is an anonymous internal class, // this points to adapter = new MyAdapter (MainActivity. this, list); listview. setAdapter (adapter) ;}}) ;}// custom adapter public class MyAdapter extends BaseAdapter {List <Map <String, Object> list; LayoutInflater inflater; public MyAdapter (Context context, List <Map <String, Object> list) {this. list = list; inflater = LayoutInflater. from (context) ;}@ Override public int getCount () {// TODO Auto-generated method stub return list. size () ;}@ Override public Object getItem (int position) {// TODO Auto-generated method stub return list. get (position) ;}@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position ;}@ Override public View getView (int position, View convertView, viewGroup parent) {// TODO Auto-generated method stub ViewHolder viewHolder; if (convertView = null) {convertView = inflater. inflate (R. layout. item, null); viewHolder = new ViewHolder (); viewHolder. tv1 = (TextView) convertView. findViewById (R. id. tv1); viewHolder. tv2 = (TextView) convertView. findViewById (R. id. tv2); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} viewHolder. tv1.setText (list. get (position ). get ("Id "). toString (); viewHolder. tv2.setText (list. get (position ). get ("Name "). toString (); return convertView ;}// helper class ViewHolder {TextView tv1; TextView tv2 ;}}

 

2. Call adapter. notifydatasetchanged () each time data is updated ();

If the content of the adapter changes, the notifyDataSetChanged method forcibly calls getView through an external method to refresh the content of each Item. (This sentence is seen on the Internet, and it is not very clear. I have read the notifydatasetchanged () source code .),This method is good when the data volume is small and the refresh frequency is slow.

The layout is the same as above. Here, only the content in MainActivity. java is sent.

Btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub map = list. get (2); map. put ("Name", "updated Name"); // The adapter is changed only here. notifyDataSetChanged ();}});}

3. Add the update function in the Custom adapter; 
This method updates the data at the specified location, for example, the second TextView of the third item in Listview, others will not be updated (via online materials and personal understanding ). The layout is the same. Here we mainly change the layout in the Custom adapter.

Btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub map = list. get (2); map. put ("Name", "updated Name"); // The adapter is changed only here. update (2, listview );}});
Public class MyAdapter extends BaseAdapter {List <Map <String, Object> list; LayoutInflater inflater; public MyAdapter (Context context, List <Map <String, Object> list) {this. list = list; inflater = LayoutInflater. from (context) ;}@ Override public int getCount () {// TODO Auto-generated method stub return list. size () ;}@ Override public Object getItem (int position) {// TODO Auto-generated method stub return list. get (position) ;}@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position;} public void update (int index, ListView listview) {// obtain the position int visiblePosition = listview of the first visible item. getFirstVisiblePosition (); // get the View at the specified position. If you are not clear about the listview cache mechanism, you can clear view View = listview. getChildAt (index-visiblePosition); ViewHolder holder = (ViewHolder) view. getTag (); holder. tv2 = (TextView) view. findViewById (R. id. tv2); setData (holder, index);} private void setData (ViewHolder holder, int index) {Map <String, Object> map = list. get (index); holder. tv2.setText (map. get ("Name "). toString () ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub ViewHolder viewHolder; if (convertView = null) {convertView = inflater. inflate (R. layout. item, null); viewHolder = new ViewHolder (); viewHolder. tv1 = (TextView) convertView. findViewById (R. id. tv1); viewHolder. tv2 = (TextView) convertView. findViewById (R. id. tv2); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} viewHolder. tv1.setText (list. get (position ). get ("Id "). toString (); viewHolder. tv2.setText (list. get (position ). get ("Name "). toString (); return convertView ;}}

Only these two places have changed.

Related Article

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.