Button inside SimpleCursorAdapter

懵懂的女人 提交于 2019-12-02 02:44:32
Abel M.

If you want to click on an image, use an ImageButton instead of an ImageView.
You can then set the onClick attribute on the button that calls a method in your Activity.

Extend a SimpleCursorAdapter and in the bindView method add onClick listeners for each button. (Maybe make it call a method in your activity ... public void buttonClicked(int position, View v)... so you know which row and which view was clicked.

Here is an example implementation of CursorAdapter you could use.

class CustomCursorAdapter extends CursorAdapter{
    LayoutInflater inflater;

    public CustomCursorAdapter(Context context, Cursor c, int flags){
        super(context,c,flags);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor){
        int row_id = cursor.get('_id');  //Your row id (might need to replace)
        ImageButton button = (ImageButton) view.findViewById(R.id.champPlusone);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                //ADD STUFF HERE you know which row is clicked. and which button
            }
        });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.beerdrunk, parent, false);
        bindView(v,context,cursor);
        return v;
    }

}

;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!