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;
}
}
Related Questions
Sponsored Content
39 Answered Questions
[SOLVED] How to add dividers and spaces between items in RecyclerView?
- 2014-07-07 20:01:19
- EyesClear
- 558328 View
- 887 Score
- 39 Answer
- Tags: android android-recyclerview divider
48 Answered Questions
[SOLVED] How do I "select Android SDK" in Android Studio?
- 2015-12-18 10:16:53
- delete
- 433615 View
- 1026 Score
- 48 Answer
- Tags: android android-studio eclipse-project-file
3 Answered Questions
[SOLVED] How to catch onclick event in Fragment
- 2013-05-15 23:35:17
- chipbk10
- 2902 View
- 2 Score
- 3 Answer
- Tags: java android android-layout android-intent
3 Answered Questions
[SOLVED] Saving ToggleButton state in ListView by using SharedPreferences
- 2016-02-26 11:10:59
- Ethan
- 846 View
- 2 Score
- 3 Answer
- Tags: android listview sharedpreferences baseadapter togglebutton
1 Answered Questions
[SOLVED] Listview with custom checkable rows backed by custom cursor adapter looses selection when recycling views
- 2013-05-02 03:43:13
- Azimoto
- 1809 View
- 3 Score
- 1 Answer
- Tags: android android-listview android-cursoradapter custom-adapter android-checkbox
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
- 2014-06-23 16:36:00
- Atarang
- 370 View
- 0 Score
- 1 Answer
- Tags: android listview android-cursoradapter
2 comments
@viv 2011-12-08 11:15:21
Try some thing like this :
@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:And
DeleteEntryOnClicklistener
should look something like this:@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