如何使用 CursorAdapter 从 ListView 中删除所选项目
我正在使用 CursorAdapter,下面是我的适配器类。我的列表由两个文本视图和每行一个按钮组成。现在,单击按钮后,我想从列表和数据库中删除所选项目。如何从数据库中获取所选项目的 id,以便将其删除,然后通知适配器(刷新列表)。
public class MyAdapter extends CursorAdapter {
Cursor c;
LayoutInflater inflater;
Context context;
private String TAG = getClass().getSimpleName();
public MyAdapter(Context context, Cursor c) {
super(context, c);
this.c = c;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
TextView txtName = (TextView) view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_username)));
TextView txtPassword = (TextView) view.findViewById(R.id.txt_password);
txtPassword.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_password)));
Button button = (Button) view.findViewById(R.id.btn_delete);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.d(TAG, "Button Click ");
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.row, null);
return v;
}
}
I am using CursorAdapter
and below is my adapter class. My list consists of two text views and one button on each row. Now, on click of the button I want to delete the selected item from the list as well as from the database. How can I get the id of the selected item from the database so that I can delete it and then notify the adapter (refresh the list).
public class MyAdapter extends CursorAdapter {
Cursor c;
LayoutInflater inflater;
Context context;
private String TAG = getClass().getSimpleName();
public MyAdapter(Context context, Cursor c) {
super(context, c);
this.c = c;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
TextView txtName = (TextView) view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_username)));
TextView txtPassword = (TextView) view.findViewById(R.id.txt_password);
txtPassword.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_password)));
Button button = (Button) view.findViewById(R.id.btn_delete);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.d(TAG, "Button Click ");
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.row, null);
return v;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情:
Try some thing like this :
我假设这个 ID 在光标中。然后只需创建自己的类DeleteEntryOnClicklistener,它实现OnClickListener 并让它在其构造函数中获取ID,并在单击时删除该条目。
如果我误解了您的问题或者我不清楚,请发表评论,欢呼:)
编辑:
在您的
bindView()
中,将 OnClicklistener 更改为如下所示:DeleteEntryOnClicklistener
应该看起来像这样:Im assuming this ID is in the cursor. Then simply make your own class DeleteEntryOnClicklistener which implements OnClickListener and let it take the ID in its constructor, and deletes the entry when clicked.
Please comment if I have misunderstood your problem or if Im being unclear, cheers :)
edit:
In your
bindView()
, change the OnClicklistener to something like this:And
DeleteEntryOnClicklistener
should look something like this: