McLovin
McLovin

Reputation: 3417

Accessing SQLite record from ListView item?

I display the user data from SQLite database using a custom CursorAdapter. Each ListView item is actually a layout with many TextViews.

in the CursorAdapter.bindView() method I get database data from the Cursor, and display their modified, prettier versions to the user. Here's what I mean:

public void bindView(View view, Context context, Cursor cursor) {
    //get listview items Views
    TextView dayTextView = (TextView)view.findViewById(R.id.listview_item_day);
    //...

    //get data from cursor
    int day = cursor.getInt(1);
    //...

    //display data nicely to the user
    String sDay = "Day number " + String.valueOf(day) " was a very nice day";

    dayTextView.setText(sDay);
}

I want that when the user long clicks an item (I'll use OnItemLongClickListener for that), the item will be deleted (easy peasy) but I also want to delete the day record from the database.

When the ListView item gets clicked, I have the next parameters supplied to me (in OnItemLongClickListener):

AdapterView<?> adapterView, View view, int position, long id

How do I get to the database record from all these parameters? I could use TextView.getText() and apply some string manipulation methods to the result (remember I added strings to the data before setting it in ListView), but I feel like there's another, more clever way.

Upvotes: 0

Views: 20

Answers (1)

Meikiem
Meikiem

Reputation: 1936

You might use a List<Integer> days = new Arraylist<>(); to keep days you are modifying and so the index of the list alway will be same as position and you can get your day content by days.get(position) and get item from your database.

you should define your list out of bindView

Upvotes: 1

Related Questions