Im attempting to develop an android program that has a user select an item from a listmenu……then pass the ID of this item to an intent to a second class, the ID is then stored in a variable and is inputted into a DBhelper method to get the name and age of a person and display this data in their relevant EditTexts in the XML.
Everything works fine until I try to start the second intent. Im getting a error of:
01-11 22:21:28.448: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteexample/com.example.sqliteexample.DisplayContents}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Note that I have initialized the ‘id’ variable passed with ‘0’. this isn’t being passed to the class methods to get the name and age?
Hopefully someone can give me a push in the right direction.
again
Here’s my code. 2 method in my DB helper class to get the name and age from the DB:
public String showData(long id) {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + id, null, null, null, null);
if(c != null)
{
// move to the selected row
c.moveToFirst();
String namePass = c.getString(1);
return namePass;
}
return null;
}
public String showAge(long id) {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + id, null, null, null, null);
if(c != null)
{
// move to the selected row
c.moveToFirst();
String agePass = c.getString(2);
return agePass;
}
return null;
Class for the listview, passing the id variable and starting the intent:
public void onListItemClick(ListView list, View v, int list_posistion, long item_id)
{
long id = item_id;
Intent i = new Intent("com.example.sqliteexample.DisplayContents");
i.putExtra("passID", id);
startActivity(i);
}
Last class which opens from the intent and displays the name and age on the onCreate:
package com.example.sqliteexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.content.Intent;
public class DisplayContents extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaycontents);
long id = 0;
EditText namePassed;
EditText agePassed;
namePassed = (EditText) findViewById(R.id.nameShow);
agePassed = (EditText) findViewById(R.id.ageShow);
HotOrNot display = new HotOrNot(this, null, null);
Bundle extras = getIntent().getExtras();
if (extras != null) {
id = extras.getLong("passedID");
}
display.open();
String returnedName = display.showData(id);
String returnedAge = display.showAge(id);
namePassed.setText(returnedName);
agePassed.setText(returnedAge);
Can you spot your error?
It can be wise to store
Extra-names as constants to eliminate typos