如何在 Android 中设置微调器值运行时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11656098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:23:26  来源:igfitidea点击:

How to set spinner values runtime in Android

androidandroid-spinner

提问by dominic

I want to set the android spinner item values at runtime.

我想在运行时设置 android spinner 项目值。

Here is what I have so far:

这是我到目前为止所拥有的:

final ArrayAdapter<String> calsListAdapter = new ArrayAdapter<String>( this,
                android.R.layout.simple_list_item_1, calendarNames);
eventCalendarList.setAdapter(calsListAdapter);
eventCalendarList.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        calendarChoosen = availableCals.get(arg2);
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        Logger.d("Cal Choosen", "fffffffffffffff");
    }
});

private List<AvailableCalendar> availableCals = new ArrayList<AvailableCalendar>();
private AvailableCalendar calendarChoosen;

But the values are not set. How can it be done?

但未设置值。怎么做到呢?

回答by MH.

I'm going to assume here that you create the Spinner's adapter based on a List<CharSequence>or something similar. You can use that to do lookups for selection, e.g.:

我将在这里假设您基于 aList<CharSequence>或类似的东西创建 Spinner 的适配器。您可以使用它来进行选择查找,例如:

String name = model.getName();
int index = list.indexOf(name);
if (index != -1) spinner.setSelection(index);

Obviously, if your model does not contain any 'name' data, then there's nothing to select in the Spinner, so you may want to add some logic to handle that. If the model does have a 'name', then find its index in the original list that was used as data source for the adapter. Only if an occurrence was found, set the selection of the spinner to that same index.

显然,如果您的模型不包含任何“名称”数据,那么在 Spinner 中就没有可供选择的内容,因此您可能需要添加一些逻辑来处理它。如果模型确实有“名称”,则在用作适配器数据源的原始列表中查找其索引。仅当发现出现时,才将微调器的选择设置为相同的索引。

回答by Balaji

I set the spinner values programatically like this:

我像这样以编程方式设置微调器值:

public class MainActivity extends Activity {
    private SharedPreferences prefs;
    private String prefName = "spinner_value";
    int id=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final List<String> list=new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        final Spinner sp=(Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
                                    android.R.layout.simple_list_item_1,list);
        adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adp);

        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        id=prefs.getInt("last_val",0);
        sp.setSelection(id);

        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, 
                View arg1,int pos, long arg3) {

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            //---save the values in the EditText view to preferences---
            editor.putInt("last_val", pos);

            editor.commit();

            Toast.makeText(getBaseContext(), 
                sp.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub                   
        }
    });               
}