Android checkbox的listView具體操作方法
本文主要實(shí)現(xiàn)在自定義的ListView布局中加入CheckBox控件,通過判斷用戶是否選中CheckBox來(lái)對(duì)ListView的選中項(xiàng)進(jìn)行相應(yīng)的操作。通過一個(gè)Demo來(lái)展示該功能,選中ListView中的某一項(xiàng),然后點(diǎn)擊Button按鈕來(lái)顯示選中了哪些項(xiàng)。
1、程序結(jié)構(gòu)圖如下:

其中Person.java是實(shí)體類,MainActivity.java是Activity組件類。listitem.xml是自定義的列表每項(xiàng)布局文件。
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實(shí)體類源碼如下:
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();
}
}
});
}
/**
* 模擬數(shù)據(jù)
*/
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;
}
}
程序運(yùn)行后的結(jié)果如下:

希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android ListView ImageView實(shí)現(xiàn)單選按鈕實(shí)例
- android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
- Android中ListView綁定CheckBox實(shí)現(xiàn)全選增加和刪除功能(DEMO)
- Android MVP模式ListView中嵌入checkBox的使用方法
- Android編程之listView中checkbox用法實(shí)例分析
- Android中ListView結(jié)合CheckBox實(shí)現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
- Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
- Android在listview添加checkbox實(shí)現(xiàn)原理與代碼
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
相關(guān)文章
Kotlin基礎(chǔ)學(xué)習(xí)之循環(huán)和異常
最近在學(xué)習(xí)kotlin,Kotlin 是一個(gè)基于 JVM 的新的編程語(yǔ)言,下面這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之循環(huán)和異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12
Android對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android如何對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android Studio kotlin生成編輯類注釋代碼
這篇文章主要介紹了Android Studio kotlin生成編輯類注釋代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03
flutter Toast實(shí)現(xiàn)消息提示框
這篇文章主要為大家詳細(xì)介紹了flutter Toast實(shí)現(xiàn)消息提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Bootstrap 下拉菜單.dropdown的具體使用方法
這篇文章主要介紹了Bootstrap 下拉菜單.dropdown的具體使用方法,詳細(xì)講解下拉菜單的交互,有興趣的可以了解一下2017-10-10
Android實(shí)現(xiàn)清除應(yīng)用緩存功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)清除應(yīng)用緩存功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

