我设置了下面的 adapter 类,包含 CursorAdapter 方法。在列表中包含两个文本视图,并在每行设置一个按钮。现在点击按钮时,我想从 list 中删除选择的项目,同时在数据库中也删除。
如何从数据库中获得选项的id,那样我就可以删除选项,然后告知adapter,刷新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;
}
}