I have a TextView and a String randomWord. When the app starts I open my db and call a method db.open() which opens it and then the db.randomize() which calls the randomize() method and queries the db for random entry and returns it as a String. Thereafter I just want to MyTextView.setText (randomWord); but when app is running nothing is displaying. I have tried to hardcode in setText (“whatever”) and that is shown.
So I guess I have done wrong in the randomize or something because I don’t get a word from the db.
This is what I try to do:
db.open();
db.randomize(generatedWord);
//text = new SpannableString(generatedWord);
//text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);
wordHolder.setText("" + generatedWord);
db.close();
This is my random method:
public Cursor randomize(String word) {
@SuppressWarnings("unused")
Cursor cursor;
return cursor = this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
}
My DB has two columns: “_id” and “word”
"create table tblnames (_id integer primary key autoincrement, "
+ "words text not null);"
Can it have to do that I don’t specify the _id and word somewhere in the randomize()?
ALSO: I tried setText (generatedWord) as well as setText (” +generatedWord);
ALSO: no errors while running.
You aren’t returning a String with your
randomize()method.This should give you the desired affect
And in your DB you need to return the string value of the column you want from your cursor, not the cursor itself. http://developer.android.com/reference/android/database/Cursor.html#getString(int)