I have a little problem with getting the elements in my sqlite database in android.My database looks like this :
id / serverName / objectId / objectOid / ....//objectId is actually userId-which I need to compare
I’m receiving an data from the server and I need to check the id of user from the server and local user id from the database.I have this piece of code :
public static Integer lUserIdByServerUserId(int serverUserId, String serverName){
return localUserIdByServerUserId( serverUserId, serverName);
}
private static Integer localUserIdByServerUserId(int serverUserId, String serverName){
DataBaseHelper dbHelper = new DataBaseHelper(context, "ops_sys_tpl.sqlite",null,1);
String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1";
ArrayList<String> result = new ArrayList<String>();
cursor = dbHelper.executeSQLQuery(query);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
result.add(cursor.getString(cursor.getColumnIndex("objectId")));
cursor.moveToNext();
}
Log.i("result ","Result : "+result.toString());
Log.i("CURSOR ","Cursor Position : "+cursor.getPosition());
Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("objectId")));
Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid);
cursor.close();
return uuid;
}
I am trying to check all the entries in database and if there is a match return it.I’m calling this method like this :
uuId = rpc.lUserIdByServerUserId(userId,newServerName);
and i get this exception at this line : Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("objectId")));
09-20 16:55:47.647: WARN/System.err(31031): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-20 16:55:47.647: WARN/System.err(31031): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
09-20 16:55:47.647: WARN/System.err(31031): at java.util.ArrayList.get(ArrayList.java:311)
09-20 16:55:47.647: WARN/System.err(31031): at com.stampii.stampii.comm.rpc.RPCCommunicator.localUserIdByServerUserId(RPCCommunicator.java:1083)
09-20 16:55:47.647: WARN/System.err(31031): at com.stampii.stampii.comm.rpc.RPCCommunicator.lUserIdByServerUserId(RPCCommunicator.java:1066)
09-20 16:55:47.657: WARN/System.err(31031): at com.stampii.stampii.comm.rpc.InfoStartRPCPacket.executeBefore(InfoStartRPCPacket.java:176)
09-20 16:55:47.657: WARN/System.err(31031): at com.stampii.stampii.user.UserLogin$2$1.run(UserLogin.java:212)
09-20 16:55:47.657: WARN/System.err(31031): at java.lang.Thread.run(Thread.java:1102)
09-20 16:55:47.657: WARN/ERROR(31031): Error - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
And actually I have only one test entry in my database which is the same as the userID return from server.
Any ideas where is my mistake and how to fix that?
Problem was due to cursor’s position
Try this code for position the cursor to start point.