I am trying to get values of rows from requested columns using cursor but I am not getting that how to do it,what is the index inside cursor.getLong(?) ,Here is my code it works but I don't how is it working? Please help.
private Message cursorToMessage(Cursor cursor) {
Message message = new Message();
message.setId(cursor.getLong(0));
message.setmessage(cursor.getString(1));
message.setthreadid(cursor.getLong(0));
return message;
}
the index is the position of the column in your projection of the query.
do if you have in your projection that you want columns id,message,threadid
then you would do
long id = cursor.getLong(0);
String message = cursor.getString(1);
long threadId = cursor.getLong(2);
the proper way to get a column so that you dont mix the indexes up would be to do this
cursor.getLong(cursor.getColumnIndex("id"));
I dont know if you did this or not but you should also be checking if the cursor has anything in it.
if(cursor != null && cursor.moveToFirst()){
message.setId(cursor.getLong(0));
message.setmessage(cursor.getString(1));
message.setthreadid(cursor.getLong(0));
}