menu_on_top
menu_on_top

Reputation: 2613

Clickable button in my database list

i have a list with data from my database. I want to have a button in every list item that onClick will delete this item from the database. Here is how i get the data from the db and present them in a list:

 DB entry=new DB(this); 

       entry.open();  

       Cursor cursor = entry.getData();
       startManagingCursor(cursor);

       ListView list=(ListView)findViewById(R.id.list);

       String[] columns = new String[] { DBHelper.NAME, DBHelper.SURNAME};

       int[] to = new int[] { R.id.textView01,R.id.textView02};  

       SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);

       list.setAdapter(mAdapter);

       entry.close();  

So,my problem is : How to create a clickable button in every list item

EDIT: this is the adapter i created:

public class myAdapter extends BaseAdapter {
    private Context mContext;

    final Drawable delete_btn;
    private ImageButton imageButton;

    private LayoutInflater inflater;

    private List<ITEMS> items = new ArrayList<ITEMS>();

    public myAdapter(Context ctx) {

        mContext = ctx;
        inflater = LayoutInflater.from(mContext);

        delete_btn = ctx.getResources()
                .getDrawable(R.drawable.delete_btn);
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        View btv = null;
        try {
            btv = inflater.inflate(R.layout.row, null);

            TextView name = (TextView) btv.findViewById(R.id.textView01);
            name.setText(DBHelper.NAME);


            TextView surname = (TextView) btv.findViewById(R.id.textView02);
            surname.setText(DBHelper.SURNAME);

            imageButton = (ImageButton) btv.findViewById(R.id.delete_btn);
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

            Toast.makeText(mContext, "Button pressed", Toast.LENGTH_LONG).show();



                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
        return btv;
    }

    public void addItem(ITEM it) {
        items.add(it);
    }

    public void setListItems(List<ITEM> lit) {
        items = lit;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int arg0) {
        return items.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

}

Upvotes: 0

Views: 146

Answers (1)

Seshu Vinay
Seshu Vinay

Reputation: 13588

create your own CustomCursorAdapter instead of SimpleCursorAdapter. Here is an example

Upvotes: 1

Related Questions