load from spinner sqlite with text and value

前端 未结 1 1989
情话喂你
情话喂你 2021-02-02 02:58

i have a spinner load data to sqlite

i have field id and field name in database.

private void loadSpinnerDataHama() {
        // database handler
                


        
1条回答
  •  孤城傲影
    2021-02-02 03:41

    It is really simple, what you can do is to implement/ represent what you need by a class, in the following manner :

    Create the following class : SpinnerObject

    public class SpinnerObject {
    
        private  int databaseId;
        private String databaseValue;
    
        public SpinnerObject ( int databaseId , String databaseValue ) {
            this.databaseId = databaseId;
            this.databaseValue = databaseValue;
        }
    
        public int getId () {
            return databaseId;
        }
    
        public String getValue () {
            return databaseValue;
        }
    
        @Override
        public String toString () {
            return databaseValue;
        }
    
    }
    

    Using this class, you can still use the Android implementation of the ArrayAdapter (no need to implement your own, since the toString method provides the string value that you want to display, and you still store the database id that you also need).

    Now you will have to create your list as follows :

    public List < SpinnerObject> getAllLabels(){
        List < SpinnerObject > labels = new ArrayList < SpinnerObject > ();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_LABELS;
    
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
    
        // looping through all rows and adding to list
        if ( cursor.moveToFirst () ) {
            do {
                labels.add ( new SpinnerObject ( cursor.getString(0) , cursor.getString(1) ) );
            } while (cursor.moveToNext());
        }
    
        // closing connection
        cursor.close();
        db.close();
    
        // returning labels
        return labels;
    }
    

    Now that you have the list of objects with the values and ids, you load the spinner this way :

    private void loadSpinnerDataHama() {
        // database handler
        DatabaseSpinner db = new DatabaseSpinner(getApplicationContext()); 
        // Spinner Drop down elements
        List  lables = db.getAllLabels();
        // Creating adapter for spinner
        ArrayAdapter dataAdapter = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, lables);
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // attaching data adapter to spinner
        spin2.setAdapter(dataAdapter);
    }
    

    And the spinner will display the values, while having their ids (from the database) too.

    In order to retrieve the id of the currently selected item, you do this :

    int databaseId = Integer.parseInt ( ( (SpinnerObject) spin2.getSelectedItem () ).getId () );
    

    Please try it and let me know what happens.

    0 讨论(0)
提交回复
热议问题