This article describes the usage of checkbox in listView in Android programming. Share it for your reference, as follows:
We often use checkbox in listView. After not replying to the application directly, you will find that the OnItemClickListener event in ListView will conflict with the selection event in checkbox. How to deal with this? Directly upload the code.
list_item.xml code:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_while"> <TextView android: android:layout_height="50dp" android:layout_width="fill_parent" android:gravity="center_vertical" android:textColor="@color/color_black" android:layout_marginLeft="8dp" /> <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_alignParentRight="true" android:layout_marginRight="8dp" android:focusable="false" <!--This must be added,Otherwise there will be conflicts--> android:clickable="false" <!--This must be added,Otherwise there will be conflicts--> /> </RelativeLayout>
Code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class Add_note_tag_list_BaseAdapter extends BaseAdapter { private LayoutInflater inflater; private List<noteTag> list_notetag_date; //Define whether the multi-check box is selected public static Map<Integer, Boolean> isSelected; public Add_note_tag_list_BaseAdapter(Context context,List<noteTag> list_notetag_date) { = (context); this.list_notetag_date = list_notetag_date; //Define here isSelected. This map records the status of each listitem, and the initial states are all false. isSelected = new HashMap<Integer, Boolean>(); for (int i = 0; i < list_notetag_date.size(); i++) { (i, false); } } @Override public int getCount() { // TODO Auto-generated method stub return list_notetag_date.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list_notetag_date.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) { add_note_noteTag notetag = null; if(convertView==null) { convertView = (.add_note_tag_list_item, null); notetag = new add_note_noteTag(); notetag.txt_add_note_tag_list_name = (TextView)(.txt_add_note_tag_list_name); notetag.chk_add_note_tag_list_chk = (CheckBox)(.chk_add_note_tag_list_chk); (notetag); } else { notetag = (add_note_noteTag)(); } notetag.txt_add_note_tag_list_name.setText(list_notetag_date.get(position).get_tagName()); notetag.chk_add_note_tag_list_chk.setChecked((position)); return convertView; } public class add_note_noteTag { TextView txt_add_note_tag_list_name; CheckBox chk_add_note_tag_list_chk; } }
Application page:
list_popwin_note_tag.setAdapter(new Add_note_tag_list_BaseAdapter(this, list_noteTag_date)); list_popwin_note_tag.setOnItemClickListener(new noteTagListItemOnClickListener());
/** * Click on the list item event * @author cg * */ class noteTagListItemOnClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { // TODO Auto-generated method stub add_note_noteTag vHollder = (add_note_noteTag) (); //Every time you get the clicked item, the checkbox status will be changed, and the map value will be modified at the same time. vHollder.chk_add_note_tag_list_chk.setChecked(vHollder.chk_add_note_tag_list_chk.isChecked()==true ? false : true); boolean check = vHollder.chk_add_note_tag_list_chk.isChecked(); Add_note_tag_list_BaseAdapter.(position, check); ("noteTagListItemOnClickListener", list_noteTag_date.get(position).get_tagName() + check); } }
I hope this article will be helpful to everyone's Android programming design.