How to update a spinner dynamically?

后端 未结 10 1134
北荒
北荒 2020-12-08 02:32

I\'ve been trying to update my spinner in android dynamically but nothing I try has been working.

This is the following code I\'m using to update the spinner.

相关标签:
10条回答
  • 2020-12-08 03:13

    Change the underlying data and call the notifyDataSetChanged() on adapter.

       list.clear();
      list.add("A");
          list.add("B");
      dataAdapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-12-08 03:15

    Actually, you either have to call clear/add on the adapter, or create and set a new adapter. The adapter does not retain a reference to your list (it is only calling toArray on your list at construction), so there is no way for it to update itself.

    dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, newStringList);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCategory.setAdapter(dataAdapter);
    
    0 讨论(0)
  • 2020-12-08 03:20

    You only need to call setAdapter() once and you call adapter.notifyDataSetChanged() to update the data.

    0 讨论(0)
  • 2020-12-08 03:22

    Is there a typo or something? Which is the difference between dbAdapter and adapter. If the Spinner has already the adapter, you don't have to re-assign it. More over, the only think you have to do is update the adapter and call notifyDataSetChanged method.

    typeList = adapter.getList(); //array list with the values
    // change the values, and then
    adapter.notifyDataSetChanged();
    
    0 讨论(0)
提交回复
热议问题