检索在旋转被采摘的对象是什么 [英] Retrieving what object was picked in a spinner

查看:118
本文介绍了检索在旋转被采摘的对象是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我看着在微调API,但它使用一个AdapterView说我的事件处理,但我从未创建过调整。我创建了string.xml数组,然后我用检索
安卓:项= @阵列/ arrayName中的

So I looked in the spinner api but it says I event handle using an AdapterView but I never created an adapted. I created the array in string.xml and then I retrieved using Android:entries = @array/arrayname

那么,如何找回被创造什么没有适配器的微调选择了用户?还是我在看这个问题?

So how do I retrieve what the user picked in the spinner without the Adapter being created? Or am I looking at this wrong?

推荐答案

谷歌是指通过实施适配器视图什么是你必须实现你的活动的 OnItemSelectedListener 接口如:

What Google means by implementing an AdapterView is that you have to implement its OnItemSelectedListener interface on your activity such as:

public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
    ...

    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

,你必须设置的事件侦听器的微调为:

and you have to set the event listener for that spinner as:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

下面是一个完整的例子:

Here's a full example:

MainActivity

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {

    //Spinner
    private Spinner spinner;
    //The array that will store the spinner items
    private ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Create the spinner and add its listener
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);

        //Spinner items, stored as a String array
        list = new ArrayList<>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");

        //To populate the spinner with a list of choices, you need to specify a SpinnerAdapter in your Activity or Fragment
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
        //Set the adapter to the spinner
        spinner.setAdapter(adapter);


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        //To Get the selected item:
        //Since the spinner items are stored inside an array
        //we can get the selected item text such as item.get(position)
        Toast.makeText(this, list.get(position), Toast.LENGTH_LONG).show();
    }

activity_main XML布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:background="#CCC"
        />

</LinearLayout>

这篇关于检索在旋转被采摘的对象是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆