【问题标题】:How to implement check box in listView Android to delete listItems如何在listView Android中实现复选框以删除listItems
【发布时间】:2011-07-26 18:19:08
【问题描述】:

这是我的列表布局:

<?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 都是可点击的(如转到不同的意图),但是当我在我的活动中单击一个按钮(删除按钮)时,我希望能够显示复选框并选择要删除的项目并将其删除。示例代码表示赞赏。

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);
}

【问题讨论】:

  • 这在 SO 上已经被问过很多次了。

标签: android listview checkbox


【解决方案1】:

删除选中的项目

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()

Since: API Level 1 通知附加视图底层数据 已更改,它应该会自行刷新。

【讨论】:

  • 我将如何删除位置 i 处的项目?
  • 抱歉,我可能对此不太清楚。我只需要从我的列表中删除它。有没有办法或办法做到这一点?
  • 我正在使用这个:List&lt;HashMap&lt;String, String&gt;&gt; painItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); ListView listthings;
  • 非常感谢,但仍然存在一个问题。我不能有一个有效的复选框并单击列表中的项目。如果我添加复选框,我什至无法再单击该项目。有什么原因,有解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多