CheckBox列表的使用实例

本文介绍如何在Android应用中使用ListView组件实现多选功能,包括设置ListView的多选模式、创建自定义适配器以及处理单个条目的选择状态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

importjava.util.ArrayList;

importandroid.app.Activity;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.View.OnClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.CheckBox;
importandroid.widget.Checkable;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.TextView;

importcom.sec.android.touchwiz.samples.R;

publicclassListLayoutSample3XmlextendsActivity{

ListViewmListView;
privatefinalStringLOG_TAG_LISTLAYOUTSAMPLE3="ListLayoutSample3Xml";


/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listlayoutsample3xml_main);

mListView=(ListView)findViewById(R.id.contactCheckList);
//[09.10.23]ListView?choiceMode?CHOICE_MODE_MULTIPLE?????
//???item?check??????
//[E]SetListView'schoiceModetoCHOICE_MODE_MULTIPLEformulticheck.
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

ArrayList<ContactItem>list=newArrayList<ContactItem>();

for(inti=0;i<mName.length;i++){
ContactItemcontact=newContactItem(mThumbs<wbr style="line-height:25px">,mName<wbr style="line-height:25px">);<br style="line-height:25px"> list.add(contact);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> //ContactAdapter??<br style="line-height:25px"> ContactAdaptercAdapter=newContactAdapter(this,R.layout.listlayoutsample3xml_row,list);<br style="line-height:25px"> //ListView?ContactAdapter???<br style="line-height:25px"> mListView.setAdapter(cAdapter);<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> /*<br style="line-height:25px"> *ContactItem????Thumbnaildata<br style="line-height:25px"> */<br style="line-height:25px"> privateInteger[]mThumbs={<br style="line-height:25px"> R.drawable.listlayoutsample3xml_1,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_2,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_3,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_4,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_5,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_seankingston,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_karina,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_jayz,<br style="line-height:25px"> R.drawable.listlayoutsample3xml_madonna};<br style="line-height:25px"><br style="line-height:25px"> /*<br style="line-height:25px"> *ContactItem????FullNamedata<br style="line-height:25px"> */<br style="line-height:25px"> privateString[]mName=<br style="line-height:25px"> {<br style="line-height:25px"> "AnnaBay",<br style="line-height:25px"> "AgathaChristina",<br style="line-height:25px"> "AgnesGreen",<br style="line-height:25px"> "Mom",<br style="line-height:25px"> "July",<br style="line-height:25px"> "SeanKingston",<br style="line-height:25px"> "Karina",<br style="line-height:25px"> "Jay-Z",<br style="line-height:25px"> "Madonna"<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> /*<br style="line-height:25px"> *ListView?????Adapter.ArrayAdapter???????.<br style="line-height:25px"> *ContactItemtype?data??ArrayList????.<br style="line-height:25px"> */<br style="line-height:25px"> privateclassContactAdapterextendsArrayAdapter&lt;ContactItem&gt;{<br style="line-height:25px"><br style="line-height:25px"> privateArrayList&lt;ContactItem&gt;items;<br style="line-height:25px"><br style="line-height:25px"> publicContactAdapter(Contextcontext,inttextViewResourceId,ArrayList&lt;ContactItem&gt;items){<br style="line-height:25px"> super(context,textViewResourceId,items);<br style="line-height:25px"> this.items=items;<br style="line-height:25px"> }<br style="line-height:25px"> @Override<br style="line-height:25px"> publicViewgetView(intposition,ViewconvertView,ViewGroupparent){<br style="line-height:25px"> Viewv=convertView;<br style="line-height:25px"> if(v==null){<br style="line-height:25px"> LayoutInflatervi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br style="line-height:25px"> v=vi.inflate(R.layout.listlayoutsample3xml_row,null);<br style="line-height:25px"> }<br style="line-height:25px"> ContactItemti=items.get(position);<br style="line-height:25px"> if(ti!=null){<br style="line-height:25px"> ImageViewthumbview=(ImageView)v.findViewById(R.id.photo1);<br style="line-height:25px"> TextViewnameview=(TextView)v.findViewById(R.id.text1);<br style="line-height:25px"> finalCheckBoxcheck=(CheckBox)v.findViewById(R.id.checkbox1);<br style="line-height:25px"><br style="line-height:25px"> if(thumbview!=null){<br style="line-height:25px"> thumbview.setImageResource(ti.getContactThumb());<br style="line-height:25px"> }<br style="line-height:25px"> if(nameview!=null){<br style="line-height:25px"> nameview.setText(ti.getFullName());<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> if(check!=null){<br style="line-height:25px"> finalintmyPosition=position;<br style="line-height:25px"> check.setOnClickListener(newOnClickListener(){<br style="line-height:25px"><br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> check.post(newRunnable(){<br style="line-height:25px"> publicvoidrun(){<br style="line-height:25px"> booleanfchecked=check.isChecked();<br style="line-height:25px"> onBtnCheckChanged(myPosition,fchecked);<br style="line-height:25px"> }<br style="line-height:25px"> });<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> });<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> returnv;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privatevoidonBtnCheckChanged(intposition,booleancheck){<br style="line-height:25px"> Log.d(LOG_TAG_LISTLAYOUTSAMPLE3,"onBtnCheckChanged("+position+","+check+")");<br style="line-height:25px"> mListView.setItemChecked(position,check);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> /**<br style="line-height:25px"> *@authorcooldawn.kim<br style="line-height:25px"> *<br style="line-height:25px"> *ContactItem<br style="line-height:25px"> */<br style="line-height:25px"> classContactItem{<br style="line-height:25px"> privateIntegermContactThumb;<br style="line-height:25px"> privateStringmFullName;<br style="line-height:25px"><br style="line-height:25px"> publicContactItem(Integerthumb,StringfullName)<br style="line-height:25px"> {<br style="line-height:25px"> this.mContactThumb=thumb;<br style="line-height:25px"> this.mFullName=fullName;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicIntegergetContactThumb(){<br style="line-height:25px"> returnmContactThumb;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicStringgetFullName(){<br style="line-height:25px"> returnmFullName;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }//classContactItem<br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticclass</span><span style="color:#ff6600; line-height:25px">ItemView</span><span style="color:#3366ff; line-height:25px">extendsLinearLayoutimplementsCheckable{<br style="line-height:25px"><br style="line-height:25px"> publicItemView(Contextcontext,AttributeSetattrs){<br style="line-height:25px"> super(context,attrs);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicbooleanisChecked(){<br style="line-height:25px"> CheckBoxcheck=findCheckBox();<br style="line-height:25px"> if(check!=null){<br style="line-height:25px"> returncheck.isChecked();<br style="line-height:25px"> }<br style="line-height:25px"> returnfalse;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicvoidsetChecked(booleanchecked){<br style="line-height:25px"> Log.d("ItemView","setChecked("+checked+")");<br style="line-height:25px"> CheckBoxcheck=findCheckBox();<br style="line-height:25px"> if(check!=null){<br style="line-height:25px"> check.setChecked(checked);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicvoidtoggle(){<br style="line-height:25px"> CheckBoxcheck=findCheckBox();<br style="line-height:25px"> if(check!=null){<br style="line-height:25px"> check.toggle();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> privateCheckBoxfindCheckBox(){<br style="line-height:25px"> Viewcheck=findViewById(R.id.checkbox1);<br style="line-height:25px"> if(check!=null&amp;&amp;checkinstanceofCheckBox){<br style="line-height:25px"> return(CheckBox)check;<br style="line-height:25px"> }<br style="line-height:25px"> returnnull;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }</span><br style="line-height:25px"> }</wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值