android - 使用简单光标适配器时如何设置按钮 onclicklistener

标签 android listview simplecursoradapter

  • 这是我用来显示自定义 ListView 的代码,使用简单的 CursorAdapter
  • 我正在使用此代码显示购物车商品,我想在 ListView 中添加按钮
  • 由于我是 android 开发的新手,所以我无法弄清楚问题出在哪里

myDbHelper.openDataBase();

    final Cursor ictemp = myDbHelper.getOrdredItems(myDbHelper);
    if (ictemp != null) {

        ictemp.moveToFirst();
        count = ictemp.getCount();
        Log.d("count", "count===" + count);

        String[] from = new String[] { "item_name", "item_rate", "qty",
                "unit" };
        int[] to = new int[] { R.id.tv_Name, R.id.tv_Rate, R.id.et_qty,
                R.id.tv_unit };

        final SimpleCursorAdapter sc = new SimpleCursorAdapter(this,
                R.layout.list_row2, ictemp, from, to, 0);

        final Cursor crs = myDbHelper.getTotal(myDbHelper);
        if (crs != null) {

            crs.moveToFirst();

            String total = crs.getString(0);
            Gtotal.setText("Rs." + total);
            tvcount.setText("" + count);



        }

        lv.setAdapter(sc);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    final int pos, long arg3) {


                remove = (Button) arg0.findViewById(R.id.btn_remove);
                switch (arg0.getId()) {

                case R.id.btn_remove:

                    Toast.makeText(ctx, "Clicked on " + pos,
                            Toast.LENGTH_SHORT).show();

                    break;}



            }
        });

最佳答案

一个解决方案是创建一个扩展 SimpleCursorAdapterCustomAdapter
然后覆盖bindView方法
bindView 中,找到 Button 然后处理 onClickEvent

public class CustomAdapter extends SimpleCursorAdapter {

    private Context mContext;
    private Context appContext;
    private int layout;
    private Cursor cr;
    private final LayoutInflater inflater;

    public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.cr = c;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        ...
        TextView tv_Name = (TextView) view.findViewById(R.id.tv_Name);
        tv_Name.setText(...);
        ...
        Button btnRemove = (Button) view.findViewById(R.id.btn_remove);
        btnRemove.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // button click
                // Remove your item here 

            }
        });
    }
}

Activity 中使用

 final CustomAdapter sc = new CustomAdapter(this,R.layout.list_row2,ictemp, from, to, 0);
 lv.setAdapter(sc)

希望对你有帮助

关于android - 使用简单光标适配器时如何设置按钮 onclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36619579/

相关文章:

c# - 如何以编程方式选择 ListView 项目?

c# - ListView.SelectedItem 样式 - Windows 应用商店应用

arrays - 根据 SimpleRow 的选定日期设置 calendar.selectedDate

java - 在 Android 中使用 SimpleCursorAdapter 填充 ListView

android - Android 上的 PublisherAdView loadAd 性能问题

java - 在SD卡中添加多张图片

java - 无法解析符号动画

android - 在特定模拟器中安装Apk

android - 如果 SimpleCursorAdapter 关闭,则 ListView 为空()

android - 在 SimpleCursorAdapter 中 findViewById() 时为 nullPointer