gpt4 book ai didi

Android 复选框删除

转载 作者:行者123 更新时间:2023-11-29 20:09:30 24 4
gpt4 key购买 nike

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

我想做的就是在每个复选框上放置一个监听器,以便在选中时将其从 ListView 中删除并从数据库中删除。我有一个函数,当它被传递给 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
}
}

});

最佳答案

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

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

关于Android 复选框删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com