0

Hello stackoverflow community, I have a problem in Android especially with SQLite. I've created an App where you can create a ToDo List. I work in this app with a ContentResolver and the list is displayed in a simple ListView. Actual Layout. The List is orderd with through the Cursor LoaderManager and has a ascending order. The projection is the following:

String[] projection = {
            ToDoContract._ID,
            ToDoContract.COLUMN_IMAGE,
            ToDoContract.COLUMN_HEADLINE,
            ToDoContract.COLUMN_DESCRIPTION,
            ToDoContract.COLUMN_PRIORITY,
            ToDoContract.COLUMN_WEEKDAY,
            ToDoContract.COLUMN_DONE
    };
    String sortOrder = ToDoContract.COLUMN_WEEKDAY + " ASC";

The Weekdays are integers(1=monday, 7=sunday) because i didn't know another way to sort the weekdays.

My Problem is the following: I want to delete or edit these entries and i don't know how to get the row number of the entry. The ListView is registered for a ContextMenu. Here is an example: I get the ContextMenu position with the following code:

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;

Now I have the position inside the ListView but i need the position in the sqlite database. My normal way was that i create a Uri with an appended ID

Uri currentListUri = ContentUris.withAppendedId(ToDoContract.CONTENT_URI, index);

But since my list is ordered, the way doesn't work. The ID in SQLite might be 4 and in my ListView it's 1. Has anybody a hint how to get the ordered position?

Edit:

Just make it clear, i already get the count of my database through this method:

TableDBHelper dbHelper = new TableDBHelper(getContext());
long count = dbHelper.getTodoCount();

And getTodoCount(); is the following:

public long getTodoCount() {
SQLiteDatabase db = this.getReadableDatabase();
long count = DatabaseUtils.queryNumEntries(db, ToDoContract.TABLE_NAME);
db.close();
return count;

}

Neal Mc Beal
  • 245
  • 3
  • 16
  • If you want to get table rows see [This answer](https://stackoverflow.com/questions/669092/sqlite-getting-number-of-rows-in-a-database) – FarshidABZ Oct 05 '17 at 09:39
  • I wanna get the specific row in SQLite when i click on an item in the ListView. Example: I click in ListView on Item 1, i want row 1 in my SQLite Database deleted or edited. I have already a method to get the count of my db but it doesn't help. – Neal Mc Beal Oct 05 '17 at 09:41
  • Did you fill your list view from your database? If you did, so you have to put your row id in your list adapter and get it while clicking on items. – FarshidABZ Oct 05 '17 at 09:45
  • I have a CursorAdapter on my Listview. The curser gives me the columnindex (e.g. for the weekday) and the value inside this column. I get the ID anyways but i need the specific row number and i don't know how to do this. I've already tried what you suggest. – Neal Mc Beal Oct 05 '17 at 09:55
  • You can see [this question to](https://stackoverflow.com/questions/23791239/sqlite-delete-nth-row-android) – FarshidABZ Oct 05 '17 at 10:00
  • What you want is not the row number but the ID value. – CL. Oct 05 '17 at 10:16
  • @CL. Do you know how to get it? – Neal Mc Beal Oct 05 '17 at 14:13
  • [What's the purpose of item-id's in Android ListView Adapter?](https://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter) – CL. Oct 05 '17 at 14:36
  • @CL.You sir are the real MVP! Thank you! Can i mark the question as solved and give you some credits? – Neal Mc Beal Oct 05 '17 at 15:12

0 Answers0