I have two classes, one called SQLiteExample and it has a switch statement. in this switch is a button click listener where i want to read the resulting int into a TextView, this does not work. i can read the int into a Toast message and display it with no errors. however when reading it into a textview I don't know why it gets an error in the logcat when i do this. the TextView is registered correctly.
public class SQLiteExample extends Activity implements OnClickListener {
case R.id.bSQLnumberofrows:
PlayGame ec = new PlayGame(this);
ec.open();
int x = ec.fetchPlacesCount();
ec.close();
showNumbers.setText(x); // setText not working
// Toast message works perfectly and displays value of "x" on screen
Toast.makeText(SQLiteExample.this, "value of x " + x , Toast.LENGTH_SHORT).show();
break;
here is the logcat errors:
01-25 10:52:04.540: E/AndroidRuntime(8449): FATAL EXCEPTION: main
01-25 10:52:04.540: E/AndroidRuntime(8449): android.content.res.Resources$NotFoundException: String resource ID #0x5
01-25 10:52:04.540: E/AndroidRuntime(8449): at android.content.res.Resources.getText(Resources.java:247)
01-25 10:52:04.540: E/AndroidRuntime(8449): at android.widget.TextView.setText(TextView.java:3473)
01-25 10:52:04.540: E/AndroidRuntime(8449): at com.example.dbsample.SQLiteExample.onClick(SQLiteExample.java:127)
the line 127 is where the exception is: showNumbers.setText(x);
and the other class is PlayGame and has a method to get the number of rows in an SQLite database as an int:
public class PlayGame {
public int fetchPlacesCount() {
int count = 0;
Cursor q = ourDatabase.rawQuery("SELECT COUNT(*) from " + DATABASE_TABLE, null);
q.moveToFirst();
count = q.getInt(0);
return count;
}
You are calling the version of setText()
which accepts an int
parameter which is expected to refer to a string resource in an XML file. You need to convert your int
to a String
in order to display its value in a TextView. Note that you are doing this implicitly with "value of x " + x
when using a Toast. You can explicitly convert an int
into a String
by calling String.valueOf(x)
.