How to get the values from DB in spinner

后端 未结 3 1494
暗喜
暗喜 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:39

    How to populate a Spinner widget from a database

    Follow it, its really simple.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-03 17:00

    yes, cursor is required to fetch result set object or data as result set. You can find solution from

    Android Developers Blog

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