By Lalit Poptani


2011-12-07 13:52:06 8 Comments

I am using CursorAdapter and below is my adapter class. My list consists of two text views and one button on each row. Now, on click of the button I want to delete the selected item from the list as well as from the database. How can I get the id of the selected item from the database so that I can delete it and then notify the adapter (refresh the 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;
    }
}

2 comments

@viv 2011-12-08 11:15:21

Try some thing like this :

@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();
        }
    });
}

@Sergii 2014-01-31 14:24:42

cursor.requery() is deprecated now

@pgsandstrom 2011-12-07 14:01:10

Im assuming this ID is in the cursor. Then simply make your own class DeleteEntryOnClicklistener which implements OnClickListener and let it take the ID in its constructor, and deletes the entry when clicked.

Please comment if I have misunderstood your problem or if Im being unclear, cheers :)

edit:

In your bindView(), change the OnClicklistener to something like this:

long id = cursor.getLong(cursor.getColumnIndex(Helper.tbl_col_id));
button.setOnClicklistener(new DeleteEntryOnClicklistener(id));

And DeleteEntryOnClicklistener should look something like this:

public class DeleteEntryOnClicklistener implements View.OnClickListener {

    long id;

    public DeleteEntryOnClicklistener(long id) {
        this.id = id;
    }

    @Override
    public void onClick(View v) {
        database.deleteEntry(id);
    }

}

@Lalit Poptani 2011-12-07 14:06:00

yes you are right, my id is in the cursor and I want to fetch the id from the item that is selected, so how to do that.

@pgsandstrom 2011-12-07 14:25:51

I edited my answer a bit, perhaps it is a bit clearer now how I imagined the code.

@Lalit Poptani 2011-12-07 14:37:16

hmmm that seems fine, how about re-populating the list or refresh the list?

@Hirak Chhatbar 2014-10-24 13:03:17

well may be i came up couple of years later, but the prolem with above solution is that when once a row is deleted from the listview, its position changes. but its column id in the database doesnt as it is autoincrement

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to add dividers and spaces between items in RecyclerView?

48 Answered Questions

[SOLVED] How do I "select Android SDK" in Android Studio?

3 Answered Questions

[SOLVED] How to catch onclick event in Fragment

3 Answered Questions

1 Answered Questions

[SOLVED] How to set click listener for button in listview in Fragment onCreateView()

  • 2017-11-24 13:58:42
  • ANKIT
  • 203 View
  • 0 Score
  • 1 Answer
  • Tags:   android

1 Answered Questions

android cursor adapter list view

Sponsored Content