A simple method for button events in a ListView child control in android.
I look for information, refer to CSDN JZHowe Method A universal adapter for event handling of control in item of ListView was established.
The adapter code is as follows:
//Inheriting SimpleAdapter public class ListViewAdapter extends SimpleAdapter { //Create method unchanged public ListViewAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } //To rewrite the getView method, getView will be called when drawing ListView, see the SimpleAdapter source code specifically @Override public View getView(int position, View convertView, ViewGroup parent) { View item=super.getView(position, convertView, parent);//The View returned by the parent method is equivalent to a single item control if(mOnClickListeber!=null){//We check whether the interface we created is empty or not, and call the interface method instead of empty. This interface is designed later. //It is equivalent to passing the item control of ListView and the location of the item (the number of items) into the function. //We can manipulate the child controls in item and item through the location of the item and item. mOnClickListeber.onClick(item,position); } // Be sure to return this item, otherwise ListView cannot be displayed return item; } //Add an interface public interface onClickListener{ //Add an interface method to process item and its child controls void onClick(View view,int position); } //Create an interface object for external settings and internal calls. private onClickListener mOnClickListeber=null; //Create a method for setting interface objects out of class public void setOnClickListeber(onClickListener mOnClickListeber){ this.mOnClickListeber=mOnClickListeber; } }
Now that we have an adapter, we can turn in a ListView.
The creation of ListView Adapter is the same as Simple Adapter.
//Create the ListView adapter final ListViewAdapter simpleAdapter=new ListViewAdapter(context,listData,R.layout.music_list,new String[]{"text_musicName","text_number"},new int[]{R.id.text_musicName,R.id.text_number}); //Add adapters to listView listView.setAdapter(simpleAdapter); //Setting Event Processing Interface simpleAdapter.setOnClickListeber(new ListViewAdapter.onClickListener() { @Override public void onClick(View item, final int position) { //Set up processing events here. //Get the child controls of item, such as TextView textView=item.findViewById(R.id.text_musicName);//R.id.text_music Name is the sub-control ID in each Item layout file. //Now you can set various events for textView, such as setting click events textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listData.remove(position);//Click on textView in which item to delete which data simpleAdapter.notifyDataSetChanged();//Update the adapter to update ListView } }); //You can also add events to item } });
Well, now we have succeeded, this method may not be the best, I have a small white one, there are better ways to hope that the message let me learn, hope to progress together.