android 全选功能,android基于ListView和CheckBox实现多选和全选记录的功能

本文介绍了如何在Android应用中实现ListView与CheckBox结合,实现联系人列表的多选和全选功能,以及选中联系人后进行删除的操作。通过查询Contacts内容提供者获取数据,并自定义Adapter来展示列表项。点击CheckBox可以切换选中状态,全选CheckBox可以一键选中所有记录,点击删除按钮会弹出确认对话框并执行删除操作。
摘要由CSDN通过智能技术生成

应用开发中经常会有从数据库中读取数据显示,然后选中多条、全部记录并且删除的需求。在做定制系统联系人的时候也遇到这样的需求,下面写个简单的通过ListView和CheckBox实现多选、全选的例子。下面是具体的代码.

效果如下:

f49f25663cda73703f17d76e7d200e83.gif

MultiSelectActivity

/**

* MultiSelectActivity

*/

public class MultiSelectActivity extends Activity implements OnClickListener,

OnItemClickListener {

private static final String TAG = "MultiSelectActivity";

private ListView contactsDelList;

private Button contactsDelBtn;

private Button contactsCancelBtn;

private CheckBox selectAll;

private Cursor cursor;

private ContactsDeleteAdapter contactsDeleteAdapter;

private static final String[] PROJECTION = new String[] { Contacts._ID,

Contacts.DISPLAY_NAME };

private static final int CONTACTS_ID_INDEX = 0;

private static final int DISPLAY_NAME_INDEX = 1;

private ContactsDeleteListItemViews holderViews;

private final class ContactsDeleteListItemViews {

TextView nameView;

CheckBox delCheckBox;

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.contacts_delete_activity);

contactsDelList = (ListView) findViewById(R.id.contacts_delete_list);

contactsDelBtn = (Button) findViewById(R.id.contacts_delete_btn);

contactsCancelBtn = (Button) findViewById(R.id.contacts_cancel_btn);

selectAll = (CheckBox) (findViewById(R.id.contacts_delete_list_header)

.findViewById(R.id.select_all));

contactsDelList.setOnItemClickListener(this);

contactsDelBtn.setOnClickListener(this);

contactsCancelBtn.setOnClickListener(this);

selectAll.setOnClickListener(this);

}

@Override

protected void onResume() {

super.onResume();

refreshData();

}

// 查询数据库

private void refreshData() {

cursor = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,

null, null, null);

contactsDeleteAdapter = new ContactsDeleteAdapter(this, cursor);

contactsDelList.setAdapter(contactsDeleteAdapter);

}

class ContactsDeleteAdapter extends BaseAdapter {

Cursor cur;

Map selectedMap;

HashSet delContactsIdSet;

public ContactsDeleteAdapter(Context context, Cursor c) {

cur = c;

// 保存每条记录是否被选中的状态

selectedMap = new HashMap();

// 保存被选中记录作数据库表中的Id

delContactsIdSet = new HashSet();

for (int i = 0; i < cur.getCount(); i++) {

selectedMap.put(i, false);

}

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {

convertView = LayoutInflater.from(MultiSelectActivity.this)

.inflate(R.layout.contacts_delete_list_item, null);

holderViews = new ContactsDeleteListItemViews();

holderViews.nameView = (TextView) convertView

.findViewById(R.id.name);

holderViews.delCheckBox = (CheckBox) convertView

.findViewById(R.id.delete_list_item);

convertView.setTag(holderViews);

}

cur.moveToPosition(position);

ContactsDeleteListItemViews views = (ContactsDeleteListItemViews) convertView

.getTag();

final String name = cur.getString(DISPLAY_NAME_INDEX);

views.nameView.setText(name);

views.delCheckBox.setChecked(selectedMap.get(position));

// 保存记录Id

if (selectedMap.get(position)) {

delContactsIdSet.add(String.valueOf(cur

.getInt(CONTACTS_ID_INDEX)));

} else {

delContactsIdSet.remove(String.valueOf(cur

.getInt(CONTACTS_ID_INDEX)));

}

return convertView;

}

@Override

public int getCount() {

return cur.getCount();

}

@Override

public Object getItem(int position) {

if (cur.moveToPosition(position)) {

return cur;

} else {

return null;

}

}

@Override

public long getItemId(int position) {

return position;

}

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.select_all:

// 全选CheckBox的click事件处理

if (selectAll.isChecked()) {

for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {

contactsDeleteAdapter.selectedMap.put(i, true);

//click事件:全选checkbox被勾选则把所有的联系人ID添加到delContactsIdSet中

contactsDeleteAdapter.cur.moveToPosition(i);

contactsDeleteAdapter.delContactsIdSet.add(String

.valueOf(contactsDeleteAdapter.cur

.getInt(CONTACTS_ID_INDEX)));

}

contactsDelBtn.setEnabled(true);

} else {

for (int i = 0; i < contactsDeleteAdapter.getCount(); i++) {

contactsDeleteAdapter.selectedMap.put(i, false);

}

//click事件:全选checkbox被取消勾选则把delContactsIdSet清空

contactsDeleteAdapter.delContactsIdSet.clear();

contactsDelBtn.setEnabled(false);

}

contactsDeleteAdapter.notifyDataSetChanged();

break;

case R.id.contacts_delete_btn:

new AlertDialog.Builder(this).setTitle(

R.string.clearConfirmation_title).setMessage(

R.string.clearConfirmation).setNegativeButton(

android.R.string.cancel, null).setPositiveButton(

android.R.string.ok, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

final ProgressDialog progressDialog = ProgressDialog

.show(

MultiSelectActivity.this,

getString(R.string.clearProgress_title),

"", true, false);

final AsyncTask task = new AsyncTask() {

@Override

protected Void doInBackground(Void... params) {

// 删除记录事件处理(我这里就不写详细的代码了)

return null;

}

@Override

protected void onPostExecute(Void result) {

progressDialog.dismiss();

}

};

progressDialog.show();

task.execute();

}

}).setCancelable(true).create().show();

break;

case R.id.contacts_cancel_btn:

// 取消事件处理

break;

}

}

@Override

public void onItemClick(AdapterView> arg0, View view, int position,

long id) {

Log.i(TAG, "onItemClick");

ContactsDeleteListItemViews views = (ContactsDeleteListItemViews) view

.getTag();

views.delCheckBox.toggle();

contactsDeleteAdapter.selectedMap.put(position, views.delCheckBox

.isChecked());

contactsDeleteAdapter.notifyDataSetChanged();

// 判断是否有记录没被选中,以便修改全选CheckBox勾选状态

if (contactsDeleteAdapter.selectedMap.containsValue(false)) {

selectAll.setChecked(false);

} else {

selectAll.setChecked(true);

}

// 判断是否有记录被选中,以便设置删除按钮是否可用

if (contactsDeleteAdapter.selectedMap.containsValue(true)) {

contactsDelBtn.setEnabled(true);

} else {

contactsDelBtn.setEnabled(false);

}

}

}

布局文件:contacts_delete_activity.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/contacts_delete_list_header"

layout="@layout/contacts_delete_list_header" />

android:id="@+id/contacts_delete_list"

android:layout_width="match_parent"

android:layout_height="0dip"

android:layout_weight="1" />

style="@android:style/ButtonBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/contacts_delete_btn"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:enabled="false"

android:text="@string/delete" />

android:id="@+id/contacts_cancel_btn"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/cancel" />

代码下载地址:源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值