本文主要实现在自定义的ListVIEw布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListVIEw的选中项进行相应的 *** 作。通过一个Demo来展示该功能,选中ListVIEw中的某一项,然后点击button按钮来显示选中了哪些项。
1、程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。Listitem.xml是自定义的列表每项布局文件。
2、Listitem.xml布局文件源码如下:
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" androID:descendantFocusability="blocksDescendants"> <CheckBox androID:ID="@+ID/List.select" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/> <TextVIEw androID:ID="@+ID/List.name" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="name" androID:layout_gravity="center" androID:textSize="20dp" androID:layout_marginleft="10dp"/> <TextVIEw androID:ID="@+ID/List.address" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="Address" androID:layout_gravity="center" androID:textSize="20dp"/> </linearLayout> </linearLayout>
3、 main.xml布局文件源码如下:
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <button androID:ID="@+ID/show" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="Show"/> <ListVIEw androID:ID="@+ID/lvperson" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"/> </linearLayout>
4、Person.java实体类源码如下:
package com.andyIDea.bean; public class Person { private String name; private String address; public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getAddress() { return address; } public voID setAddress(String address) { this.address = address; } }
5、MainActivity.java类源码如下:
package com.andyIDea.ListvIEw; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.andyIDea.bean.Person; import androID.app.Activity; import androID.app.AlertDialog; import androID.content.Context; import androID.os.Bundle; import androID.util.Log; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.BaseAdapter; import androID.Widget.button; import androID.Widget.CheckBox; import androID.Widget.ListVIEw; import androID.Widget.TextVIEw; public class MainActivity extends Activity { button show; ListVIEw lv; List<Person> persons = new ArrayList<Person>(); Context mContext; Mylistadapter adapter; List<Integer> ListItemID = new ArrayList<Integer>(); /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mContext = getApplicationContext(); show = (button)findVIEwByID(R.ID.show); lv = (ListVIEw)findVIEwByID(R.ID.lvperson); initPersonData(); adapter = new Mylistadapter(persons); lv.setAdapter(adapter); show.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { ListItemID.clear(); for(int i=0;i<adapter.mChecked.size();i++){ if(adapter.mChecked.get(i)){ ListItemID.add(i); } } if(ListItemID.size()==0){ AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); builder1.setMessage("没有选中任何记录"); builder1.show(); }else{ StringBuilder sb = new StringBuilder(); for(int i=0;i<ListItemID.size();i++){ sb.append("ItemID="+ListItemID.get(i)+" . "); } AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this); builder2.setMessage(sb.toString()); builder2.show(); } } }); } /** * 模拟数据 */ private voID initPersonData(){ Person mPerson; for(int i=1;i<=12;i++){ mPerson = new Person(); mPerson.setname("Andy"+i); mPerson.setAddress("GuangZhou"+i); persons.add(mPerson); } } //自定义ListVIEw适配器 class Mylistadapter extends BaseAdapter{ List<Boolean> mChecked; List<Person> ListPerson; HashMap<Integer,VIEw> map = new HashMap<Integer,VIEw>(); public Mylistadapter(List<Person> List){ ListPerson = new ArrayList<Person>(); ListPerson = List; mChecked = new ArrayList<Boolean>(); for(int i=0;i<List.size();i++){ mChecked.add(false); } } @OverrIDe public int getCount() { return ListPerson.size(); } @OverrIDe public Object getItem(int position) { return ListPerson.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEw vIEw; VIEwHolder holder = null; if (map.get(position) == null) { Log.e("MainActivity","position1 = "+position); LayoutInflater mInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); vIEw = mInflater.inflate(R.layout.Listitem,null); holder = new VIEwHolder(); holder.selected = (CheckBox)vIEw.findVIEwByID(R.ID.List_select); holder.name = (TextVIEw)vIEw.findVIEwByID(R.ID.List_name); holder.address = (TextVIEw)vIEw.findVIEwByID(R.ID.List_address); final int p = position; map.put(position,vIEw); holder.selected.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { CheckBox cb = (CheckBox)v; mChecked.set(p,cb.isChecked()); } }); vIEw.setTag(holder); }else{ Log.e("MainActivity","position2 = "+position); vIEw = map.get(position); holder = (VIEwHolder)vIEw.getTag(); } holder.selected.setChecked(mChecked.get(position)); holder.name.setText(ListPerson.get(position).getname()); holder.address.setText(ListPerson.get(position).getAddress()); return vIEw; } } static class VIEwHolder{ CheckBox selected; TextVIEw name; TextVIEw address; } }
程序运行后的结果如下:
希望本文所述对大家学习AndroID软件编程有所帮助。
总结以上是内存溢出为你收集整理的Android checkbox的listView具体 *** 作方法全部内容,希望文章能够帮你解决Android checkbox的listView具体 *** 作方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)