SQLite - Getting data into a Spinner

The name of the picture


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.





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




Adapter.getItem()





please add some code to it.
– Rakesh Burbure
3 hours ago




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 :-



enter image description here


onItemSelected



If say GameC is selected from the dropdown then :-



enter image description here






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.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS