Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Populate Spinner from Sqlite and display the associated record of selected or populated item

Populate Spinner from Sqlite and display the associated record of selected or populated item

Scheduled Pinned Locked Moved Android
databasesqlite
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Varma Lanke
    wrote on last edited by
    #1

    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

    L D 2 Replies Last reply
    0
    • V Varma Lanke

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your SELECT statement is requesting all records. If you only want certain ones then add a qualifying clause.

      1 Reply Last reply
      0
      • V Varma Lanke

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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 of String 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups