Reputation: 3182
I'm trying to retrieve the name of a persons within my database. Currently Im getting an out of bounds error.
I know that the data exists that im trying to retrieve as I have a list view that displays all the data in the database.
IF ELSE check:
NameAppointPass.open();
String nameReturned = NameAppointPass.getName(sentID);
if(nameReturned != "")
{
Dialog d = new Dialog(this);
d.setTitle("returned");
TextView txt = new TextView(this);
txt.setText("win");
d.setContentView(txt);
d.show();
}
else
{
Dialog d = new Dialog(this);
d.setTitle("empty");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
Hopefully someone can see where I'm going wrong.
Currently I'm getting the following error:
01-21 21:41:43.753: E/AndroidRuntime(276): FATAL EXCEPTION: main
01-21 21:41:43.753: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.addAppointment}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
I am sending the ID of the data I want from the listview using the onclick 'long variable' as shown:
public void onListItemClick(ListView list, View v, int list_posistion, long item_id)
{
long idToPass = item_id;
Intent setName = new Intent("com.example.flybase2.addAppointment");
setName.putExtra("newPassedID", idToPass);
startActivity(setName);
}
This is then sent through a bundle to an object instance 'NameAppointPass' as shown:
NameAppointPass.open();
String nameReturned = NameAppointPass.getName(sentID);
The following code shows the method getName
that query's the database and should return the name stored at the selected ID but instead I get the error:
public String getName(long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
Upvotes: 0
Views: 972
Reputation: 3182
Managed to get this sorted. In the end i had incorrectly misnamed my variable for holding the passed ID.
Upvotes: 0
Reputation: 43738
If the cursor can be empty, you can check if the cursor has no data:
if(c != null && c.getCount() > 0)
To further diagnose why the cursor is empty put log statements at various places to check if the item id is still correct.
Upvotes: 2