前段时间在做一个项目的后期维护时,对方提出了把短信做成可以批量删除、、然后冥思苦想,终于想到了一个可执行方案、、那边的要求时这样的、、进入短信的草稿箱,然后点击一条短信,弹出批量删除对话框,然后有一个全选框,可以进行多项选择,进行删除、、、、
?
实现过程如下:首先建立一个主布局文件,主要有两个listview,然后是4个按钮,我们要做的是把四个按钮中的3个设为不可见,一个设为可见,listview也是只有一个可见,其他三个设为不可见
主xml文件:sms_store.xml
?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include layout="@layout/head" /> <!-- 动态显示界面 --> <LinearLayout android:id="@+id/body" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.95" android:orientation="vertical" android:background="#F5F8FD" > <ListView android:id="@+id/smslist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="visible"/> <!-- 批量删除下的列表 --> <ListView android:id="@+id/smslist2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone"/> </LinearLayout> <include layout="@layout/sms_foot" /></LinearLayout>
?sms_foot.xml文件
?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/bottomlist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/tab_botton" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_choceall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bt_background" android:gravity="center" android:text="全标记" android:textColor="#ffffffff" android:visibility="gone" android:width="50dp" android:layout_weight="1"/> <Button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bt_background" android:gravity="center" android:text="删除" android:layout_weight="1" android:textColor="#ffffffff" android:visibility="gone" android:width="50dp"/> <Button android:id="@+id/backbefore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bt_background" android:gravity="center" android:text="返回" android:layout_weight="1" android:textColor="#ffffffff" android:visibility="gone" android:width="50dp"/> </LinearLayout> <Button android:id="@+id/smsback" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bt_background" android:gravity="center" android:layout_gravity="right" android:text="返回" android:textColor="#ffffffff" android:visibility="visible" android:width="50dp"/></LinearLayout>
?接下载建立两个listview的主体,一个是普通的文本,一个是多选框的文本
smslist.xml
?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dip" android:ellipsize="end" android:singleLine="true" android:textColor="@color/black" android:text="TextView" /></LinearLayout>
?sms_delete.xml
?
<?xml version="1.0" encoding="utf-8"?><CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:ellipsize="end" android:singleLine="true" android:textColor="@color/black" android:textSize="20dip" ></CheckedTextView>
?在Activity里面建立
Button smsback, smsbackbrfore,btChoseall, btDelete;
ListView listv, listv2;
并且setContentView(R.layout.sms_store);
然后findid一下
?
smsback = (Button) findViewById(R.id.smsback);
smsbackbrfore = (Button) findViewById(R.id.backbefore);
btChoseall = (Button) findViewById(R.id.btn_choceall);// 全选按钮
btDelete = (Button) findViewById(R.id.btn_delete);// 删除按钮
listv = (ListView) findViewById(R.id.smslist);
listv2 = (ListView) findViewById(R.id.smslist2);
然后创建一个方法更新视图
?
private void updataview() {
listv.setVisibility(View.VISIBLE);
smsback.setVisibility(View.VISIBLE);
listv2.setVisibility(View.GONE);
smsbackbrfore.setVisibility(View.GONE);
btChoseall.setVisibility(View.GONE);
btDelete.setVisibility(View.GONE);
list = getData(status);
if (list == null) {
// 表示没有数据,显示数据为空
} else {
// 表示有数据
listViewAdapter = new ListViewAdapter(MsgStoreActivity.this, list,
R.layout.smslist, new String[] { "content" },
new int[] { R.id.title });
listv.setAdapter(listViewAdapter);
}
}
得到数据库的返回数据
?
private List<Map<String, String>> getData(String status) {
Cursor cursor = new DatabaseHelper().fetchData(MsgStoreActivity.this,
DataDefine.TABLE_MSG, Integer.valueOf(status));
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
int i = 1;
System.out.println(cursor.getCount());
if (cursor != null && cursor.getCount() > 0) {
// 有数据
do {
Map<String, String> map = new HashMap<String, String>();
map.put("id", cursor.getString(cursor
.getColumnIndex(DataDefine.KEY_MSG_ID_AUTO_INCREMENT)));
String s = cursor.getString(cursor
.getColumnIndex(DataDefine.KEY_MSG_CONTENT));
// if(s.length()>10){
// s=s.substring(0,10)+"......";
// }
map.put("content", (i++) + ". " + s);
list.add(map);
} while (cursor.moveToNext());
cursor.close();
return list;
} else {
cursor.close();
Map<String, String> map = new HashMap<String, String>();
map.put("id", null);
map.put("content", "当前没有短信!");
list.add(map);
return list;
}
?
}
?
当点击批量删除时
?
listv.setVisibility(View.GONE);
smsback.setVisibility(View.GONE);
listv2.setVisibility(View.VISIBLE);
smsbackbrfore.setVisibility(View.VISIBLE);
btChoseall.setVisibility(View.VISIBLE);
btDelete.setVisibility(View.VISIBLE);
ListViewAdapter adapter = new ListViewAdapter(MsgStoreActivity.this, list, R.layout.sms_delete,
new String[] { "content" }, new int[] { R.id.content });
listv2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listv2.setAdapter(adapter);
创建删除和全选按钮事件
?
btChoseall.setOnClickListener(new View.OnClickListener() {
?
@Override
public void onClick(View v) {
CheckedTextView cv;
for (int i = 0; i < listv2.getCount(); i++) {
cv = (CheckedTextView) listv2.getChildAt(i);
cv.setChecked(true);
}
?
}
});
btDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckedTextView cv;
int count = 0;
for (int i = 0; i < listv2.getCount(); i++) {
cv = (CheckedTextView) listv2.getChildAt(i);
if (cv.isChecked()) {
count = 1;
break;
}
}
?
if (count == 1)
adDelete.show();
else
showToast(MsgStoreActivity.this, "未选中信息!");
?
}
});
创建删除按钮点击后的弹出对话框事件
?
adDelete = new AlertDialog.Builder(MsgStoreActivity.this)
.setTitle("删除").setMessage("确定要删除信息?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
?
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
?
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
?
@Override
public void onClick(DialogInterface dialog, int which) {
CheckedTextView cv;
for (int i = 0; i < listv2.getCount(); i++) {
cv = (CheckedTextView) listv2.getChildAt(i);
if (cv.isChecked()) {
Map<?, ?> map = (Map<?, ?>) listv.getAdapter()
.getItem(i);
new DatabaseHelper().delete(
MsgStoreActivity.this,
Integer.valueOf((String) map.get("id")));
}
}
showToast(MsgStoreActivity.this, "信息已删除!");
updataview();
}
}).create();