【问题标题】:Android CheckBox DeleteAndroid CheckBox 删除
【发布时间】:2016-02-05 23:40:39
【问题描述】:

我遇到了一些严重的问题,我有一个列表视图,其中填充了数据库中的条目,列表视图中的每一行都有一个自定义行(TextView1|TextView2|TextView3|Checkbox)。

我要做的就是在每个复选框上放置一个侦听器,这样当它被选中时,它就会从列表视图中删除并从数据库中删除。我有一个函数可以在传递 Textview1 的值时从数据库中删除该行。

我遇到的问题是尝试从复选框甚至 textview 值中获取行 ID。我到处搜索,但仍然无法工作

复选框

Cursor cursor = db.getAllItems();

    //String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
    final 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.setClickable(false);
    //shoppingList.setChoiceMode(shoppingList.CHOICE_MODE_SINGLE);
    shoppingList.setAdapter(myCursorAdapter);

    CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
    check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.isChecked()) {
                //how do i get the row_id or the text view value?
            } else {
                //do nothing
            }
            }

        });

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    据我所知,这种实现是不可能的。您必须创建一个自定义适配器

    public class CustomAdapter extends SimpleCursorAdapter{
    
        public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
        }
    
        public View getView(final int position, View convertView, ViewGroup parent){
            View view = super.getView(position, convertView, parent);
            CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
            check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    // position will give you the position of the clicked element from where you can fetch your data
                } else {
                    //do nothing
                }
                }
    
            });
            return view;
        }
    }
    

    你可以使用它

    CustomAdapter adapter = new CustomAdapter(this, R.layout.row_layout, cursor, from, to);
    list.setAdapter(adapter);
    

    【讨论】:

      【解决方案2】:

      使用getTag() 方法。 例如,check.setTag(你想要的 id) `

      在侦听器中 int id =(Integer) Buttonview.getTag()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-07
        • 2018-11-01
        • 1970-01-01
        • 2012-01-15
        • 2018-07-05
        • 2018-01-09
        • 1970-01-01
        • 2014-04-23
        相关资源
        最近更新 更多