Skip to content
Advertisement

how to get ID Value from sqlite database (android studio java)

when i try to click some name from listview, i want it to return the ID from its name(in Toast Text), but i always getting result “0” wherever i click a name in listview, can you help me fix my code?, thanks

MainActivity.java

JavaScript

DatabaseHelper.java

JavaScript

Advertisement

Answer

Try this:

Cursor getNoteId = myDB.rawQuery("select id from notepadData where notepad like + "'" + currentNote + "'", null);

Edit:
Wait…now I’ve noticed what you return…
getColumnIndex() returns you index of certain column, where your id column is always 0 and it won’t change. You create table with two columns: id (index 0) and notepad (index 1)
You should use cursor.getInt(0) and before that call cursor.moveToFirst()
Do it like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement