hcg_21 2013-01-23 03:27 采纳率: 0%
浏览 5284
已采纳

在 ListView 中使用 CursorAdapter 移除一个选择项目

我设置了下面的 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;
    }
}
  • 写回答

3条回答

  • Baby_Bonnie 2013-01-28 02:16
    关注

    试一下:

    @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();
            }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元