how to add items to the spinner dynamically in android?

前端 未结 3 960
故里飘歌
故里飘歌 2020-12-05 10:36

how to add items to the spinner dynamically in android?

3条回答
  •  -上瘾入骨i
    2020-12-05 11:03

    You can follow this way

    public static void selectSpinnerItemByValue(Spinner spnr, long value){
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++)
    {
        if(adapter.getItemId(position) == value)
        {
            spnr.setSelection(position);
            return;
        }
    } }
    

    You can use the above like:

    selectSpinnerItemByValue(spinnerObject, desiredValue);
    

    you can also select by index directly like

    spinnerObject.setSelection(index);
    

提交回复
热议问题