Tagged with: Android adapter Android data disorder Adapter Data confusion
In development often encounter such problems, make a list, to the list of each item added a button listener events, but in the list of data many times there will be a click after the problem of confusion. We may have a solution to this problem in our program, but you may find it the first time you see this problem as unprepared as I did before.
So now we can analyze the root cause of this problem.
First, let's look at an error baseadapter.
package com.example.listdelectdemo;import java.util.arraylist;import android.content.context; import android.view.layoutinflater;import android.view.view;import android.view.view.onclicklistener;import android.view.viewgroup;import android.widget.baseadapter; Import android.widget.button;import android.widget.textview;import android.widget.toast;public class mydataadapter extends baseadapter {private context mcontext;private arraylist<string> mstrings;private layoutinflater minflater;private string mstrdata;public mydataadapter (context c, arraylist<string> s) {mContext = c;mstrings = s;minflater = layoutinflater.from (c);} @Overridepublic int getcount () {return mstrings.size ();} @Overridepublic object getitem (int position) {return mstrings.get (position);} @OverridEpublic long getitemid (int position) {return position;} @Overridepublic view getview (int position, view convertview, viewgroup Parent) {MyViewHolder viewHolder = null;if (convertview == null) { Convertview = minflater.inflate (r.layout.item, null);viewholder = new Myviewholder ();viewholder.item_button_test = (button) convertview.findviewbyid (R.id.item_button _test);viewholder.item_textview_content = (TextView) convertview.findviewbyid (R.id.item_ textview_content); Convertview.settag (Viewholder);} else {viewHolder = (Myviewholder) convertview.gettag ();} Here, take out the current item in the data set Mstrdata mstrdata = mstrings.get (position); Viewholder.item_textview_ Content.settext (Mstrdata); ViewHolder.item_button_test.setText ("click");//click-to-listen event is set for item's button VIEWHOLDER.ITEM_ Button_test.setonclicklistener (New onclicklistenER () {@Overridepublic void onclick (view v) { //here toast out of the Mstrdata is not the click of the Toast.maketext (mcontext, "You clicked-" + mStrData, Toast.length_long). Show ();}}); Return convertview;} class myviewholder {textview item_textview_content; Button item_button_test;}}
Then, we analyze the reasons, and believe that the old programmers can see the problem:
Mstrdata = Mstrings.get (position);
When the GetView method is called for the first time, the current item data in the collection is taken out to the member variable Mstrdata, and the program continues to execute:
ViewHolder.item_button_test.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//here to AST out of the Mstrdata is not the click of the Toast.maketext (Mcontext, "you clicked-" + Mstrdata, Toast.length_long). Show ();});
This adds a listener event to the item's button, but note that the program does not callback the
@Overridepublic void OnClick (View v) {//This Toast out of Mstrdata is not the one that clicked Toast.maketext (Mcontext, "you clicked-" + Mstrdata, to Ast. Length_long). Show ();}
Instead, it continues to callback the GetView method.
When the list is about to be loaded, that is, when the last callback GetView method, the member variable Mstrdata is assigned the last time,
The GetView method, then, will be re-assigned once every time the callback is mstrdata.
Then, when we click on the button, the listener's OnClick method is recalled, and the toast is executed:
Toast.maketext (Mcontext, "you clicked-" + Mstrdata, Toast.length_long). Show ();
At this time the Mstrdata is only the last time the value of the assignment, error is inevitable.
So, take a look at my workaround:
package com.example.listdelectdemo;import java.util.arraylist;import android.content.context; import android.view.layoutinflater;import android.view.view;import android.view.view.onclicklistener;import android.view.viewgroup;import android.widget.baseadapter; Import android.widget.button;import android.widget.textview;import android.widget.toast;public class mydataadapter extends baseadapter {private context mcontext;private arraylist<string> mstrings;private layoutinflater minflater;private string mstrdata;public mydataadapter (context c, arraylist<string> s) {mContext = c;mstrings = s;minflater = layoutinflater.from (c);} @Overridepublic int getcount () {return mstrings.size ();} @Overridepublic object getitem (int position) {return mstrings.get (position);} @OverridEpublic long getitemid (int position) {return position;} @Overridepublic view getview (int position, view convertview, viewgroup Parent) {MyViewHolder viewHolder = null;if (convertview == null) { Convertview = minflater.inflate (r.layout.item, null);viewholder = new Myviewholder ();viewholder.item_button_test = (button) convertview.findviewbyid (R.id.item_button _test);viewholder.item_textview_content = (TextView) convertview.findviewbyid (R.id.item_ textview_content); Convertview.settag (Viewholder);} else {viewHolder = (Myviewholder) convertview.gettag ();} Mstrdata = mstrings.get (position); ViewHolder.item_textView_content.setText (mstrdata); Viewholder.item _button_test.settext ("click");// the button for item to set the tap to listen, create a listening implementation class, and pass in the current Positionviewholder.item_button_ Test.setonclicklistener (New myadapterlistener (position)); return convertview;} class myviewholder {textview item_textview_content; Button item_button_test;} Class myadapterlistener implements onclicklistener {private int position;public myadapterlistener (Int pos) {position = pos;} @Overridepublic void onclick (view v) {toast.maketext (mcontext, "You clicked-" + Mstrings.get (position), toast.length_long). Show ();}}}
There is no problem at this time. The difference is that when you add a click event to the Item button, you create a new Myadapterlistener object each time to implement this listener. So in the new Myadapterlistener object when he passed a position as a token, so that each item will have a own listener class, we can do some of the listening class logic processing, there will be no problem of confusion.
This scheme can only be used as a reference scheme, one drawback is that when the list of data more, will create a lot of new objects, and Occupy memory. So what are some of the better options to share?
This article from "Xiao Ming's Code Road" blog, reproduced please contact the author!
The Item button on the ListView in Android listens for a problem resolution