Populate Spinner from Sqlite and display the associated record of selected or populated item
-
My Database Helper Code is Here
public class DataHelper extends SQLiteOpenHelper {
public static final String DB_NAME="myDb";
public static final String Table_Pro="student";public static final String Create_Pro="Create table if not exists "+Table_Pro+"(id integer primary key autoincrement,pname not null unique,age int,qual text)";
public static final String Delete\_Pro="Drop table if exists "+Table\_Pro; public DataHelper(Context context) { super(context, DB\_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(Create\_Pro); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(Delete\_Pro); // db.execSQL("DROP TABLE IF EXISTS "+Table\_Pro); onCreate(db); } public void insertProvince(String pname,int age,String qual) { SQLiteDatabase db=this.getWritableDatabase(); db.beginTransaction(); ContentValues values; try { values=new ContentValues(); values.put("pname",pname); values.put("age",age); values.put("qual",qual); db.insert(Table\_Pro, null, values); db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } } public ArrayList<String> getAllProvinces() { ArrayList<String> list=new ArrayList<String>(); SQLiteDatabase db=this.getReadableDatabase(); db.beginTransaction(); try { String selectQuery = " SELECT \* FROM "+ Table\_Pro; Cursor cursor=db.rawQuery(selectQuery,null); if(cursor.getCount()>0) { while (cursor.moveToNext()) { String pname=cursor.getString(cursor.getColumnIndex("pname")); list.add(pname); } } db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } return list; }
public Cursor getAllDa
-
My Database Helper Code is Here
public class DataHelper extends SQLiteOpenHelper {
public static final String DB_NAME="myDb";
public static final String Table_Pro="student";public static final String Create_Pro="Create table if not exists "+Table_Pro+"(id integer primary key autoincrement,pname not null unique,age int,qual text)";
public static final String Delete\_Pro="Drop table if exists "+Table\_Pro; public DataHelper(Context context) { super(context, DB\_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(Create\_Pro); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(Delete\_Pro); // db.execSQL("DROP TABLE IF EXISTS "+Table\_Pro); onCreate(db); } public void insertProvince(String pname,int age,String qual) { SQLiteDatabase db=this.getWritableDatabase(); db.beginTransaction(); ContentValues values; try { values=new ContentValues(); values.put("pname",pname); values.put("age",age); values.put("qual",qual); db.insert(Table\_Pro, null, values); db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } } public ArrayList<String> getAllProvinces() { ArrayList<String> list=new ArrayList<String>(); SQLiteDatabase db=this.getReadableDatabase(); db.beginTransaction(); try { String selectQuery = " SELECT \* FROM "+ Table\_Pro; Cursor cursor=db.rawQuery(selectQuery,null); if(cursor.getCount()>0) { while (cursor.moveToNext()) { String pname=cursor.getString(cursor.getColumnIndex("pname")); list.add(pname); } } db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } return list; }
public Cursor getAllDa
-
My Database Helper Code is Here
public class DataHelper extends SQLiteOpenHelper {
public static final String DB_NAME="myDb";
public static final String Table_Pro="student";public static final String Create_Pro="Create table if not exists "+Table_Pro+"(id integer primary key autoincrement,pname not null unique,age int,qual text)";
public static final String Delete\_Pro="Drop table if exists "+Table\_Pro; public DataHelper(Context context) { super(context, DB\_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(Create\_Pro); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(Delete\_Pro); // db.execSQL("DROP TABLE IF EXISTS "+Table\_Pro); onCreate(db); } public void insertProvince(String pname,int age,String qual) { SQLiteDatabase db=this.getWritableDatabase(); db.beginTransaction(); ContentValues values; try { values=new ContentValues(); values.put("pname",pname); values.put("age",age); values.put("qual",qual); db.insert(Table\_Pro, null, values); db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } } public ArrayList<String> getAllProvinces() { ArrayList<String> list=new ArrayList<String>(); SQLiteDatabase db=this.getReadableDatabase(); db.beginTransaction(); try { String selectQuery = " SELECT \* FROM "+ Table\_Pro; Cursor cursor=db.rawQuery(selectQuery,null); if(cursor.getCount()>0) { while (cursor.moveToNext()) { String pname=cursor.getString(cursor.getColumnIndex("pname")); list.add(pname); } } db.setTransactionSuccessful(); }catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); db.close(); } return list; }
public Cursor getAllDa
How about something like:
Cursor res = dataHelper.getAllData(position);
...
public Cursor getAllData(int position)
{
SQLiteDatabase db = getReadableDatabase();
Cursor res=db.rawQuery("select * from "+Table_Pro+" where id="+position,null);
return res;
}But that seems rathger silly when the data has already been retrieved into
listpro
. Why not:String s = listpro.get(position);
Of course you'd have to modify
getAllProvinces()
to store more than just an array ofString
types."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles