这是我的列表布局:
<?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:id="@+id/linearLayout1"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout2"
android:layout_height="fill_parent">
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout3" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="@+id/row1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/row2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
虽然我有多余的布局,但所有这些都是一个复选框和几个TextView。我希望每个listItem都是可点击的(如转到不同的意图),但是当我在活动中单击一个按钮(DELETE BUTTON)时,我希望能够显示复选框并选择要删除的项目并将其删除。示例代码表示赞赏
Java代码:
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
listthings.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, Item1.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
startActivity(intent);
}
答案 0 :(得分:3)
删除已检查的项目
private void deleteCheckedItems() {
int count = this.mainListView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.mainListView.isItemChecked(i)) {
painItems.remove(i)
}
}
}
更多信息:http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)
然后通知适配器更新:
adapter.notifyDataSetChanged();
public void notifyDataSetChanged()
自:API级别1通知附加的View底层数据 已被改变,它应该刷新自己。