如何获得所选项目的一个微调的价值? [英] How to get the value of a selected item in a spinner?

查看:162
本文介绍了如何获得所选项目的一个微调的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习Android应用开发,我不知道如何登录选定项的从微调的值。

I'm just learning Android app development, and I don't understand how to Log the value of a selected item from a spinner.

下面是我对 MainActivity.java

import android.content.ContentResolver;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.provider.ContactsContract;

import java.util.ArrayList;

    public class MainActivity extends ActionBarActivity {
        public Spinner contactSpinner = (Spinner) findViewById(R.id.contact_list);

    protected void onCreate(Bundle savedInstanceState) {
        ArrayList contactList = new ArrayList();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contactList.add("An item");  
        contactList.add("Another item");  
        contactList.add("A third item");  

        // Add items from contactList to spinner
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,contactList);
        contactSpinner.setAdapter(adapter);
    }

    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
    }

    contactSpinner.setOnItemSelectedListener(this);  
}

我下面就响应用户选择了Android开发文档,但我的code,我得到这个错误:无法解析符号setOnItemSelectedListener

I'm following the Android dev docs on responding to user selections, but with my code, I get this error: Could not resolve symbol setOnItemSelectedListener

为什么呢?而如何解决它,所以我能登录什么是在微调选择?

Why? And how do I fix it so I can Log what was selected in the Spinner?

这code ++工程

package com.example.compy.spookr;

公共类MainActivity扩展ActionBarActivity实现AdapterView.OnItemSelectedListener {
    公共微调contactSpinner;

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener { public Spinner contactSpinner;

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

    ArrayList contactList = new ArrayList();
    contactSpinner = (Spinner) findViewById(R.id.contact_list);
    contactSpinner.setOnItemSelectedListener(this);
    // Rest of code...
}

@Override
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);
    Log.v("onItemSelected",(String) parent.getItemAtPosition(pos));
}

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

// Rest of code ... 

}

推荐答案

首先,你需要实现您想要使用的方法接口。你的情况,你可以做这样的:

First of all, you need to implement the interface which you want to use the methods. In your case, you could do it like this:

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

然后,把 @覆盖在方法来描述你想要的行为:

Then, put @Override upon the methods to describe the behaviors you want:

  @Override
  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);

}

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

}

然后设置你的微调监听器里你的的onCreate()

And then set your spinner listener inside your onCreate():

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactSpinner = (Spinner) findViewById(R.id.contact_list);
contactSpinner.setOnItemSelectedListener(this);  
....

这篇关于如何获得所选项目的一个微调的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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