我在一个android项目上工作,我正在使用一个旋转器,它使用一个从数据库填充的数组适配器。

我不知道如何从列表中以编程方式设置所选项目。例如,如果在旋转器中有以下项目:

类别1 第二类 3级

当创建屏幕时,我如何通过编程使类别2成为所选项目。我在想它可能类似于c#的i.e. Spinner。SelectedText = "Category 2"但似乎没有任何方法类似于Android。


当前回答

我做了一些Spinner的扩展功能,用于加载数据和跟踪项目选择。

Spinner.kt

fun <T> Spinner.load(context: Context, items: List<T>, item: T? = null) {
    adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, items).apply {
        setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    }

    if (item != null && items.isNotEmpty()) setSelection(items.indexOf(item))
}

inline fun Spinner.onItemSelected(
    crossinline itemSelected: (
        parent: AdapterView<*>,
        view: View,
        position: Int,
        id: Long
    ) -> Unit
) {
    onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {
        }

        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            itemSelected.invoke(parent, view, position, id)
        }
    }
}

每天的例子

val list = listOf("String 1", "String 2", "String 3")
val defaultData = "String 2"

// load data to spinner
your_spinner.load(context, list, defaultData)

// load data without default selection, it points to first item
your_spinner.load(context, list)

// for watching item selection
your_spinner.onItemSelected { parent, view, position, id -> 
    // do on item selection
}

其他回答

Some explanation (at least for Fragments - never tried with pure Activity). Hope it helps someone to understand Android better. Most popular answer by Arun George is correct but don't work in some cases. The answer by Marco HC uses Runnable wich is a last resort due to additional CPU load. The answer is - you should simply choose correct place to call to setSelection(), for example it works for me: @Override public void onResume() { super.onResume(); yourSpinner.setSelection(pos); } But it won't work in onCreateView(). I suspect that is the reason for the interest to this topic. The secret is that with Android you can't do anything you want in any method - oops:( - components may just not be ready. As another example - you can't scroll ScrollView neither in onCreateView() nor in onResume() (see the answer here)

这是我的工作。

 spinner.setSelection(spinner_adapter.getPosition(selected_value)+1);

最优解为:

    public String[] items= new String[]{"item1","item2","item3"};
    // here you can use array or list 
    ArrayAdapter adapter= new ArrayAdapter(Your_Context, R.layout.support_simple_spinner_dropdown_item, items);
    final Spinner itemsSpinner= (Spinner) findViewById(R.id.itemSpinner);
itemsSpinner.setAdapter(adapter);

若要自动获取该项的位置,请添加以下语句

itemsSpinner.setSelection(itemsSpinner.getPosition("item2"));

这招对我很管用:

mySpinner.post(new Runnable() {
    @Override
    public void run() {
        mySpinner.setSelection(position);

        spinnerAdapter.notifyDataSetChanged();
    }
});

大多数时候spinner.setSelection(i);//i是0到(size-1)适配器的大小 是行不通的。如果你调用spinner.setSelection(i);

这取决于你的逻辑。

如果视图是完全加载的,你想从接口调用它,我建议你调用

spinner.setAdapter(spinner_no_of_hospitals.getAdapter());
spinner.setSelection(i); // i is 0 or within adapter size

或者,如果您想在活动/片段生命周期之间进行更改,请像这样调用

spinner.post(new Runnable() {
  @Override public void run() {
    spinner.setSelection(i);
  }
});