简体   繁体   English

在Android Studio中添加微调器时遇到问题

[英]Trouble adding a Spinner in Android Studio

KEEPING FOR HISTORIC. 保持历史记录。 SKIP TO EDIT. 跳过以进行编辑。

I am having trouble adding a spinner to an android app I'm developing. 我在向正在开发的android应用中添加微调器时遇到问题。 I haven't developed the code yet to go in the app, but just to do some testing I have it sending a toast message to let me know it works. 我尚未开发要在应用程序中使用的代码,但是只是为了进行一些测试,我让它发送了一条敬酒消息,让我知道它的工作原理。 According to this page: http://developer.android.com/guide/topics/ui/controls/spinner.html You can use User to create events by having OnItemSelected be called in another class. 根据此页面: 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
    }
}

That's it's own class. 那是自己的课。 I call it with this: 我这样称呼它:

    //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);

However, I get this error: SetOnItemSelectedListener(android...) in AdapterView cannot be applied to (com...Activity) 但是,我收到此错误:AdapterView中的SetOnItemSelectedListener(android ...)无法应用于(com ... Activity)

It asks me to cast it to (AdapterView.OnItemSelectedListener) but when I do I get errors because I can't cast the activity to an OnItemSelectedListener. 它要求我将其强制转换为(AdapterView.OnItemSelectedListener),但是当我收到错误消息时,因为无法将活动强制转换为OnItemSelectedListener。 What am I missing here? 我在这里想念什么? I'm a bit new to Android Programming, so I'm sorry if this is an easy answer... 我对Android编程有点陌生,所以很抱歉这是一个简单的答案...

EDIT: 编辑:

After speaking with Bhush_techidiot, he sent me to some resources that helped, however I'm having trouble finalizing my implementation. 与Bhush_techidiot交谈后,他向我提供了一些帮助的资源,但是我在完成实现时遇到了麻烦。 Now my SpinnerActivity temporarily looks like this: 现在我的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

        }
    });
}
}

but I don't know how to call SpinnerActivity from my EditJobActivity, so I'm getting the error: "... is not an enclosing class" on EditJobActivity. 但是我不知道如何从我的EditJobActivity调用SpinnerActivity,所以我在EditJobActivity上收到错误:“ ...不是一个封闭的类”。 Should I be making a new layout for this spinner? 我应该为该微调器重新布局吗?

Check the following links. 检查以下链接。 Make sure you keep your solutions as simple as you can also don't hesitate to try complicated things once you get the simple one working :) Understand the extending and implementing classes and you are good to go! 确保您的解决方案尽可能简单,一旦获得简单的解决方案,也不要犹豫尝试复杂的事情:)了解扩展类和实现类,您就可以尝试了!

  1. Android Spinner - onItemSelected / setOnItemSelectedListener not triggering Android Spinner-onItemSelected / setOnItemSelectedListener不触发
  2. setOnItemSelectedListener of Spinner does not call Spinner的setOnItemSelectedListener不调用
  3. How to get the value of a selected item in a spinner? 如何获得微调器中选定项目的值?

All the best! 祝一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM