データベースがあり、スピナーに表示できる値がいくつかあります.選択したものをテキストビューに表示するにはどうすればよいですか?
// CREATE TABLE IF NOT EXISTS
public void createTable() {
try {
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE
+ " (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
mydb.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Errore durante la creazione della tabella",
Toast.LENGTH_LONG);
}
}
// THIS FUNCTION INSERTS DATA TO THE DATABASE
public void insertIntoTable() {
try {
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Cibo e Bevande','cibo')");
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Trasporti','trasporti')");
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Svago','svago')");
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Acquisti vari','acquisti')");
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Sanità','sanità')");
mydb.execSQL("INSERT INTO " + TABLE
+ "(NAME, PLACE) VALUES('Altro','altro')");
mydb.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Error in inserting into table", Toast.LENGTH_LONG);
}
}
// THIS FUNCTION SHOWS DATA FROM THE DATABASE
public ArrayList<String> getTableValues() {
ArrayList<String> my_array = new ArrayList<String>();
try {
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
Cursor allrows = mydb.rawQuery("SELECT * FROM " + TABLE, null);
System.out.println("COUNT : " + allrows.getCount());
if (allrows.moveToFirst()) {
do {
String ID = allrows.getString(0);
String NAME = allrows.getString(1);
String PLACE = allrows.getString(2);
my_array.add(NAME);
TextView categorietext = (TextView) findViewById(R.id.sceltelabel);
categorietext.setText(NAME); // doesn't work. It display always "Altro"
} while (allrows.moveToNext());
}
allrows.close();
mydb.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Errore.",
Toast.LENGTH_LONG);
}
return my_array;
}
私は試してみました
TextView categorietext = (TextView) findViewById(R.id.sceltelabel);
categorietext.setText(NAME);
しかし、それは常に表示さAltro
れ、テキストビューで変更されない何か他のものを選択した場合..ありがとう