SQLite - Getting data into a Spinner
So here's the thing,
But the things is, is there a way when I click the data from my spinner, and get the other two column in my database and make the two TextView namely tvName, and TvCategory change? Is there a link on how you can help me? I barely need this for my project. Thank you very much.
I use SQLITE as The Database.
Adapter.getItem()
1 Answer
1
Yes you can override the Spinner's onItemSelected
method using the Spinner's setOnItemClickListener
method.
onItemSelected
setOnItemClickListener
Based upon the above then you could have a DatabaseHelper (i.e. a subclass of SQLiteOpenHelper) as per :-
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "gamesdb";
public static final int DBVERSION = 1;
public static final String TBNAME = "gameinfo";
public static final String COl_GAMEINFO_ID = BaseColumns._ID;
public static final String COL_GAMEINFO_NAME = "name";
public static final String COl_GAMEINFO_CATEGORY = "category";
public static final String COL_GAMEINFO_GAMES = "games";
SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " + TBNAME + "(" +
COl_GAMEINFO_ID + " INTEGER PRIMARY KEY, " +
COL_GAMEINFO_NAME + " TEXT, " +
COl_GAMEINFO_CATEGORY + " TEXT, " +
COL_GAMEINFO_GAMES + " TEXT " +
")";
db.execSQL(crtsql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long add(String name, String category, String games) {
ContentValues cv = new ContentValues();
cv.put(COL_GAMEINFO_NAME,name);
cv.put(COl_GAMEINFO_CATEGORY,category);
cv.put(COL_GAMEINFO_GAMES,games);
return mDB.insert(TBNAME,null,cv);
}
public Cursor getAllRows() {
return mDB.query(TBNAME,null,null,null,null,null,null);
}
}
And an activity such as :-
When first run you would get :-
onItemSelected
If say GameC is selected from the dropdown then :-
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Yes, there is ... AdapterView<T>.getItemAtPosition() (with assumption that
Adapter.getItem()
in used Adapter is implemented in the right way) asked bazilion times– Selvin
6 hours ago