【问题标题】:Android ListView With Check Box Deleting/Getting Row ID带有复选框删除/获取行ID的Android ListView
【发布时间】:2016-05-11 15:16:41
【问题描述】:

我正在尝试使用复选框实现列表视图,以便在选中复选框时删除列表行。我正在用光标填充列表视图,这工作正常并且正在显示复选框。

我遇到的问题是弄清楚如何获取已选中框的行的_id。

谁能告诉我如何实现这样的东西

ListView 和 CheckBox

        Cursor cursor = db.getAllItems();

    //String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
    String[] columns = new String[] {db.KEY_ITEM_NAME, db.KEY_MEASUREMENT, db.KEY_UNIT};

    int[] to = new int[] {R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};

    final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row4, cursor, columns, to, 0);

    final ListView shoppingList = (ListView) findViewById(R.id.shoppingList);
    shoppingList.setAdapter(myCursorAdapter);


    CheckBox deleteCheck = (CheckBox)findViewById(R.id.checkBox1);

    deleteCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (isChecked){
                // How do I get the list item clicked to delete row?
            }
        }

    });

XML - Row.4.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/ingredientName"
    android:layout_width="wrap_content"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:hint="wewewe"/>

<TextView
    android:id="@+id/ingredientMeasurement"
    android:textColor="#000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/ingredientName"
    android:padding="5dp"
    android:hint="fefefef"/>

<TextView
    android:id="@+id/ingredientUnit"
    android:textColor="#000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/ingredientMeasurement"
    android:padding="5dp"
    android:hint="qqqqq"/>


<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="#fff"
    android:text=""/>
</LinearLayout>

【问题讨论】:

  • 好吧,您可以创建两种从列表视图中删除项目的方法,一种在适配器中,第二种在数据库中。 adapter.remove(item.getId(checkedId)); 并从数据库中调用第二个方法 dataBase.remove(item.getId(checkedId)); 这就是您从适配器和数据库中删除项目的方式。
  • 感谢回复,只有一个问题,getId 函数,这会得到该行的id 还是数据库中实际的_id
  • getId 函数将获取您在 listView 中选择的对象的指定 id。

标签: android listview checkbox


【解决方案1】:

使用此自定义光标适配器并在此适配器中处理 onCheckedChanged。

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SimpleCursorAdapter;

public class CustomAdapter extends SimpleCursorAdapter implements CompoundButton.OnCheckedChangeListener {

    private Context mContext;
    private Context appContext;
    private int layout;
    private Cursor cr;
    private final LayoutInflater inflater;

    public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.cr = c;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        CheckBox checkBox1 = (CheckBox) view.findViewById(R.id.checkBox1);
        checkBox1.setOnCheckedChangeListener(this);
        int columnIndex = cursor.getColumnIndex(your column here);
        int columnvalue = cursor.getInt(columnIndex);
        checkBox1.setTag(columnvalue);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int id = (Integer) buttonView.getTag();
    }
}

【讨论】:

  • 感谢您的回复,我有点困惑如何将其实现到列表活动中?
  • 你正在使用 SimpleCursorAdapter 只需用这个 CustomAdapter 替换它,我希望它会工作。
  • 还要在 bindView 方法中设置成分名称、成分测量和成分单元的值,并且 columnValue 将是您在选中复选框时想要获取的 id。
  • 我正在尝试在 onCheckChange 方法中做一个简单的 Toast,以查看 id 是否正在更改,但无论按下哪一行,它都保持不变,我无法删除特定行,我我错过了一些东西,因为它每行都会发生变化??
  • checkBox1.setTag(columnvalue);检查这一行在 bindView 方法中设置了什么标签,是相同还是正在改变。
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 2011-09-15
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 2011-02-14
  • 2016-02-03
相关资源
最近更新 更多