如何实现复选框的ListView的Andr​​oid删除时listItems [英] How to implement check box in listView Android to delete listItems

查看:97
本文介绍了如何实现复选框的ListView的Andr​​oid删除时listItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的名单上的布局:

 < XML版本=1.0编码=UTF-8&GT?;
    <的LinearLayout
       的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
         机器人:方向=垂直
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =FILL_PARENT>
     < LinearLayout中的android:layout_width =FILL_PARENT机器人:ID =@ + ID / linearLayout1
         机器人:layout_height =WRAP_CONTENT>
    < LinearLayout中的android:layout_width =WRAP_CONTENT机器人:ID =@ + ID / linearLayout2
         机器人:layout_height =FILL_PARENT>
    <复选框机器人:ID =@ + ID / checkBox1机器人:layout_width =WRAP_CONTENT
     机器人:layout_height =WRAP_CONTENT>< /复选框>
    < / LinearLayout中>
    < LinearLayout中的android:layout_width =WRAP_CONTENT机器人:ID =@ + ID / linearLayout3机器人:layout_height =FILL_PARENT机器人:方向=垂直>
        < TextView的Andr​​oid版本:文本=TextView的机器人:ID =@ + ID / ROW1机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
        < TextView的Andr​​oid版本:文本=TextView的机器人:ID =@ + ID / ROW2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
    < / LinearLayout中>
    < / LinearLayout中>

< / LinearLayout中>
 

虽然我有一个多余的布局,这一切是一个复选框和几个TextViews的。我希望每个的listItem为可点击(如转到不同的意图),但是当我点击我的活动按钮(删除按钮),我希望能够表现出的复选框,并选择要删除的项目并删除它。样品code AP preciated。

Java的code:

 适配器=新SimpleAdapter(这一点,painItems,R.layout.mylistlayout,
            从到);
    listthings.setAdapter(适配器);
    listthings.setOnItemClickListener(本);
    公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
    意向意图=新的意图(这一点,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(意向);
}
 

解决方案

删除选中的项目

 私人无效deleteCheckedItems(){
    诠释计数= this.mainListView.getAdapter()getCount将()。
    的for(int i = 0; I<计数;我++){
        如果(this.mainListView.isItemChecked(i))的{
            painItems.remove㈠
        }
    }
}
 

更多信息:的http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)

然后通知适配器更新:

  adapter.notifyDataSetChanged();
 

  

公共无效notifyDataSetChanged()

     

自:API级别1通知所附认为,基础数据   已被改变​​和它应该刷新本身

This is my list layout:

<?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>

Although I have an excess of Layouts, all this is is a checkbox and a couple of TextViews. I want each listItem to be clickable(as in go to a different intent), but when I click a button (DELETE BUTTON)in my activity, I want to be able to show the checkboxes and select the items to delete and delete it. Sample code appreciated.

Java code:

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

解决方案

Delete checked items

private void deleteCheckedItems() {
    int count = this.mainListView.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.mainListView.isItemChecked(i)) {
            painItems.remove(i)
        }
    }
}

More info: http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)

and then notify the adapter to update:

adapter.notifyDataSetChanged();

public void notifyDataSetChanged ()

Since: API Level 1 Notifies the attached View that the underlying data has been changed and it should refresh itself.

这篇关于如何实现复选框的ListView的Andr​​oid删除时listItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆