How to get the values from DB in spinner

后端 未结 3 1497
暗喜
暗喜 2021-02-03 16:16

How to get values into the spinner from the DB. I want to display values or datas on UI from DB. Is it cursor necessary to use to populate the values onto the UI?

3条回答
  •  难免孤独
    2021-02-03 16:44

    I just follow the things to get the values from database, when the spinner values changed by postion -

    Java class

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    //set some adapter here
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    
    public class MyOnItemSelectedListener implements OnItemSelectedListener 
    {
    @SuppressWarnings("unused")
    public void onItemSelected(AdapterView parent,View view, int pos, long id)
    {
    try
        {
        switch(parent.getId())
        {
        case R.id.spinner2:
    
            int selected_spinner_item_position=pos;
    
            String expired_spinner_item =parent.getItemAtPosition(pos).toString();
            if(selected_spinner_item_position==0)
            {
                //am just using simple cursor adapter here and getting the values from database using dh.fetchAll() when the position is 0
                sc_adapter(dh.fetchAll());
                spinnertemp = selected_spinner_item_position;
            }
            }
        }
    }
    }
    

    Database

    public Cursor fetchAll()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        cursor = db.query(t1, new String[] {"_id",task, pname, prior, time, dateformat, duptitle,dupname}, "stat=0", null, null, null, prior);
        return cursor;
    }
    

    Just do what you need like these steps.

提交回复
热议问题