Android的动态微调更新动态、Android

2023-09-08 09:08:17 作者:爱过人渣怪我眼瞎

我与Android和纱厂工作,我需要一些帮助。我有一个创建两个微调器和一个按钮的类。第一个微调是对我的类,我的第二个是我的子类。我所试图做的是动态更新第二微调(spinner2)。我一直在尝试使用adapter2.clear(),但Android的崩溃,并显示错误无法启动componentinfo不支持的经营活动

I'm working with Android and Spinners and I need some help. I have a class that creates two spinners and a button. The first spinner is for my category, my second is for my sub-category. What I am trying to do is dynamically update the second spinner (spinner2). I've been trying to use adapter2.clear() but that crashes android, with an error "unable to start activity componentinfo unsupported operation"

下面是我的code:

public class MyClass extends MyBaseClass
{
    int category;
    int sub_category;
    ArrayAdapter<String> adapter2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quizes);

        //CATEGORY INFO
        final String[] items1 = new String[] {"One", "Two", "Three"};
        final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, items1);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);

        //SUBCATEGORY INFO
        final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
        final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        adapter2 = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, items2);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.button1);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // do something when the button is clicked



                startActivity(new Intent(MyClass.this, GoToOtherClass.class));
              } 
        });


        //SELECTOR CONTROL FOR SPINNER1 {CATEGORY}
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                MyClass.this.category = spinner1.getSelectedItemPosition();

        //OTHER STUFF


            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        //SELECTOR CONTROL FOR SPINNER2 {SUB-CATEGORY}
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                MyClass.this.sub_category = spinner2.getSelectedItemPosition();

        //OTHER STUFF

            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }
}

我了解的.clear()/添加()方法,但只要我尝试清除()/加()我的程序崩溃,做什么我需要做修复的东西,所以我可以更改spinner2内容我的子类别列表?任何意见会有所帮助,因为我已经花了几个小时做的事情,如:

I understand about the .clear()/.add() methods, but anytime I try clear()/add() my program crashes, what do I need to do to fix things so I can change the spinner2 contents for my sub-category list? Any advice would help, as I have spent hours doing things such as:

对象T = adapter2.getitem(0);spinner2.remove((字符串)T);

Object t=adapter2.getitem(0); spinner2.remove((String) t);

或adapter2.clear()和其他一些技巧和我还有没有进一步的想法。我仍然在学习机器人。我试着寻找一些在这里其他职位上的计算器和谷歌,但不知道如何表达自己的思想工作。

or adapter2.clear() and a few other tricks and I have no further ideas left. I am still learning android. I've tried looking at some other posts here on stackoverflow and google but was not sure how to get their ideas working.

推荐答案

在您更改第二微调的内容,你需要调用 adapter2.notifyDataSetChanged()。如果没有呼叫,用户界面​​不会与微调的新内容更新,你可能也有问题,引用的东西不存在了。

After you change the contents of the second Spinner, you need to call adapter2.notifyDataSetChanged(). Without that call, the UI won't update with the new contents of the Spinner, and you could also have problems referencing things that don't exist anymore.