首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索在微调器中拾取的对象

检索在微调器中拾取的对象
EN

Stack Overflow用户
提问于 2014-12-08 07:30:25
回答 1查看 415关注 0票数 0

所以我查看了微调api,但它显示我使用AdapterView处理事件,但我从未创建过自适应的。我在string.xml中创建了数组,然后使用Android进行检索:entries= @ array /arrayname

那么,如何在不创建Adapter的情况下检索用户在微调器中拾取的内容?还是我看错了?

EN

回答 1

Stack Overflow用户

发布于 2014-12-08 07:41:48

谷歌实现AdapterView的意思是,你必须在你的活动上实现它的OnItemSelectedListener接口,例如:

代码语言:javascript
复制
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
    }
}

并且您必须将该微调器的事件侦听器设置为:

代码语言:javascript
复制
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

下面是一个完整的示例:

MainActivity

代码语言:javascript
复制
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布局

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27349153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档