简体   繁体   中英

Refresh the current item of a Spinner

Consider the following combo list

comboList = new Spinner(this);
list_arr = new ArrayList<String>();

The ArrayList is filled with Strings from SharedPreferences and the Spinner is populated in this way

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list_arr);
comboList.setAdapter(dataAdapter);

Then it gets updated in case of an event OnClickListener()

                list_arr.clear();
                ArrayList<String> res = getMyLists();
                for (int i = 0; i < res.size(); i++) {
                    list_arr.add(res.get(i));
                }

How can I refresh also the already selected item programmatically? From the GUI, I have to manually select another value from the list and then change it back.

This could be a duplicate of this other question but it is very old and unanswered.

您可能必须调用dataAdapter.notifyDataSetChanged();

In this case, you'll need to re-create the spinner's adapter:

list_arr.clear();

ArrayList<String> res = getMyLists();
for (int i = 0; i < res.size(); i++) {
 list_arr.add(res.get(i));
}

dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_arr);
comboList.setAdapter(dataAdapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM