无法在Android Studio中添加Spinner

时间:2015-09-12 19:50:23

标签: java android-studio listener android-arrayadapter android-spinner

保持历史。跳转编辑。

我无法在我正在开发的Android应用中添加微调器。我还没有开发代码进入应用程序,但只是做一些测试我发送一个Toast消息让我知道它的工作原理。根据此页面:http://developer.android.com/guide/topics/ui/controls/spinner.html您可以使用User通过在另一个类中调用OnItemSelected来创建事件。

public class SpinnerActivity extends EditJobActivity implements AdapterView.OnItemSelectedListener {
    @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)
        Toast.makeText(SpinnerActivity.this, "It worked", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

这就是它自己的课程。我用它来称呼它:

    //Prepare the first (Job Discovery) spinner
    Spinner mJobDiscovery = (Spinner) findViewById(R.id.SpinJobDiscovered);
    // Create an ArrayAdapter using the string array and a default spinner layout
    JobDiscoveryAdapter = ArrayAdapter.createFromResource(this,
            R.array.spin_JobDiscoveryHome, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    JobDiscoveryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    mJobDiscovery.setAdapter(JobDiscoveryAdapter);
    mJobDiscovery.setOnItemSelectedListener(this);

但是,我收到此错误: AdapterView中的SetOnItemSelectedListener(android ...)无法应用于(com ... Activity)

它要求我将其强制转换为(AdapterView.OnItemSelectedListener),但是当我这样做时,我会收到错误,因为我无法将活动强制转换为OnItemSelectedListener。我在这里错过了什么?我对Android编程有点新手,所以如果这是一个简单的答案我很抱歉...

编辑:

在与Bhush_techidiot交谈后,他向我发送了一些有用的资源,但是我无法完成我的实施。现在我的SpinnerActivity暂时看起来像这样:

public class SpinnerActivity extends EditJobActivity {

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

        /*for fill your Spinner*/
    List<String> SpinnerArray = new ArrayList<String>();
    SpinnerArray.add("Item 1");
    SpinnerArray.add("Item 2");
    SpinnerArray.add("Item 3");
    SpinnerArray.add("Item 4");
    SpinnerArray.add("Item 5");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, SpinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner spinner = (Spinner) findViewById(R.id.SpinJobDiscovered);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            // TODO Auto-generated method stub
            Object item = arg0.getItemAtPosition(arg2);
            if (item != null) {
                Toast.makeText(EditJobActivity.this, item.toString(),
                        Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(EditJobActivity.this, "Selected",
                    Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
}
}

但我不知道如何从我的EditJobActivity调用SpinnerActivity,所以我在EditJobActivity上收到错误:“......不是一个封闭的类”。我应该为这个微调器做一个新的布局吗?

1 个答案:

答案 0 :(得分:1)

检查以下链接。确保你的解决方案尽可能简单,一旦你完成简单的工作,就不要犹豫尝试复杂的事情:)理解扩展和实施的课程,你很高兴!

  1. Android Spinner - onItemSelected / setOnItemSelectedListener not triggering
  2. setOnItemSelectedListener of Spinner does not call
  3. How to get the value of a selected item in a spinner?
  4. 一切顺利!