本文介绍了如何从ListView中使用的CursorAdapter删除所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的CursorAdapter
键,下面是我的适配器类。我的列表包括两个文本视图和每一行上的一个按钮。现在,对点击按钮的欲从列表以及从数据库中删除选择的项目。我怎样才能从数据库中选择项目的ID,这样我可以删除它,然后通知适配器(刷新列表)。
公共类MyAdapter扩展的CursorAdapter {
光标C;
LayoutInflater充气;
上下文语境;
私人字符串变量=的getClass()getSimpleName()。
公共MyAdapter(上下文的背景下,光标C){
超(背景下,C);
this.c = C;
this.context =背景;
充气= LayoutInflater.from(上下文);
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,最终光标光标){
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按钮=(按钮)view.findViewById(R.id.btn_delete);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
Log.d(TAG,点击);
}
});
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
视图V = inflater.inflate(R.layout.row,NULL);
返回伏;
}
}
解决方案
尝试一些像这样的事情:
@覆盖
公共无效bindView(查看视图,上下文的背景下,最终光标光标){
TextView的txtName的=(的TextView)view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(cursor.getColumnIndex(助手
.tbl_col_username)));
TextView的txtPassword =(TextView中)view.findViewById(R.id.txt_password);
txtPassword.setText(cursor.getString(cursor.getColumnIndex(助手
.tbl_col_password)));
最后弦乐的itemId = cursor.getString(cursor.getColumnIndex(ID));
Button按钮=(按钮)view.findViewById(R.id.btn_delete);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
Log.d(TAG,点击);
deleteRecordWithId(的itemId);
cursor.requery();
notifyDataSetChanged();
}
});
}
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;
}
}
解决方案
Try some thing like this :
@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)));
final String itemId = cursor.getString(cursor.getColumnIndex("id"));
Button button = (Button) view.findViewById(R.id.btn_delete);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.d(TAG, "Button Click ");
deleteRecordWithId(itemId);
cursor.requery();
notifyDataSetChanged();
}
});
}
这篇关于如何从ListView中使用的CursorAdapter删除所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!