Android应用开发之多选删除之二
MultiSelectDeleteActivity.java
[java]
package cn.lynn.multisel;
import java.util.HashMap;
import java.util.HashSet;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class MultiSelectDeleteActivity extends Activity implements OnClickListener, OnItemClickListener {
private static final String TAG = "MultiSelectDeleteActivity";
private ListView delList;
private Button delBtn;
private Button canBtn;
private CheckBox selAllChk;
private Cursor cursor;
private DeleteListAdapter delListAdapter;
// 期望查询的联系人字段
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;
// 用于保存记录是否被选中的状态,这里定义为静态变量是为了防止Home键切换之后普通成员变量无法保存切换之前的数据
private static HashMap<Integer, Boolean> statusMap;
/**
* 用于删除的联系人记录的封装类
*/
private final class ViewHolder {
TextView nameTxt;
CheckBox selectChk;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusMap = new HashMap<Integer, Boolean>();
delList = (ListView) findViewById(R.id.del_list);
delBtn = (Button) findViewById(R.id.del_btn);
canBtn = (Button) findViewById(R.id.can_btn);
selAllChk = (CheckBox) findViewById(R.id.select_all).findViewById(R.id.select_all_chk);
delList.setOnItemClickListener(this);
delBtn.setOnClickListener(this);
canBtn.setOnClickListener(this);
selAllChk.setOnClickListener(new SelectAllClickListener());
}
@Override
protected void onDestroy() {
super.onDestroy();
statusMap.clear();
// 静态变量,提示和加快GC回收它,否则有很大可能要等到VM Full gc时才能回收
statusMap = null;
}
@Override
public void onResume() {
super.onResume();
refreshData();
}
/**
* 查询联系人数据库记录
*/
private void refreshData() {
selAllChk.setEnabled(false);
cursor = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION, null, null, null);
if(cursor.getCount() > 0) {
selAllChk.setEnabled(true);
}
Log.i(TAG, "cursor= " + cursor);
delListAdapter = new DeleteListAdapter(this, cursor);
delList.setAdapter(delListAdapter);
}
/**
* 单独为全选CheckBox设置点击事件的易做图,因为它的布局是从当前Activity的子布局文件R.id.select_all中加载而来,
* 这样做的好处是防止多个按钮都在Activity布局层次上监听同类点击事件时出现“屏蔽”现象
*/
private class SelectAllClickListener implements OnClickListener {
@Override
public void onClick(View v) {
if(selAllChk.isChecked()) {
for (int i = 0; i < delListAdapter.getCount(); i++) {
statusMap.put(i, true);
delListAdapter.cursor.moveToPosition(i);
delListAdapter.idSet.add(delListAdapter.cursor.getInt(CONTACTS_ID_INDEX) + "");
}
delBtn.setEnabled(true);
} else {
for (int i = 0;
补充:移动开发 , Android ,