简体   繁体   English

Android Multiple Spinner更新

[英]Android Multiple Spinner Updates

Okay, so I'm working on an android app. 好的,我正在开发一个Android应用。 In one of my app's activities (called convert), there are already 3 blank spinners (We'll ignore the third one for now). 在我应用程序的一项活动(称为转换)中,已经有3个空白微调器(我们现在将忽略第三个空白微调器)。 One main spinner lets the user decide on a topic such as temperature, pressure, or volume. 一个主微调器使用户可以决定一个主题,例如温度,压力或体积。

public class Convert extends Activity{
    String category = MySingleton.getInstance().getCategory();
    protected DataStorage appState;
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.convert);

        appState = (DataStorage)getApplication();

        // Category Info
        Spinner convertTopic = (Spinner) findViewById(R.id.convertTopic);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.convertTopic_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        convertTopic.setAdapter(adapter);

        category = MySingleton.getInstance().getCategory();
        // Subcategory Info
        Spinner inputUnit = (Spinner) findViewById(R.id.inputUnit);
        ArrayAdapter<CharSequence> adapter2;
        adapter2 = ArrayAdapter.createFromResource(this, R.array.blank_array, android.R.layout.simple_spinner_item);
        if(category.equals("Temperature"))
            adapter2 = ArrayAdapter.createFromResource(this, R.array.convertUnitTemperature_array, android.R.layout.simple_spinner_item);
        else if(category.equals("Pressure"))
            adapter2 = ArrayAdapter.createFromResource(this, R.array.convertUnitPressure_array, android.R.layout.simple_spinner_item);
        else if(category.equals("Volume"))
            adapter2 = ArrayAdapter.createFromResource(this, R.array.convertUnitVolume_array, android.R.layout.simple_spinner_item);

        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        inputUnit.setAdapter(adapter2);

        convertTopic.setOnItemSelectedListener(new OnTopicSelectedListener());
    }
}

I also have a separate class that handles my first spinner selection. 我还有一个单独的类来处理我的第一个微调器选择。

public class OnTopicSelectedListener implements OnItemSelectedListener{
    String category = MySingleton.getInstance().getCategory();
    protected DataStorage appState;

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        if(parent.getItemAtPosition(pos).toString().length() != 0){
            Toast.makeText(parent.getContext(), "Working with " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }
       category = parent.getItemAtPosition(pos).toString();
       MySingleton.getInstance().setCategory(category);
    }

    @SuppressWarnings("rawtypes")
    public void onNothingSelected(AdapterView parent){
        // Do nothing.
    }
}

My issue is rather simple but I haven't found a solution to it. 我的问题很简单,但是我没有找到解决方案。 Whenever that activity starts, everything is blank as expected. 每当该活动开始时,所有内容都为空白。 However, whenever someone selects an option from the main spinner convertTopic , the second spinner won't update. 但是,只要有人从主微调器convertTopic中选择一个选项,第二个微调器就不会更新。

The only way I can get the second spinner to update is to return to the previous activity and go back to the Convert activity. 我可以更新第二个微调器的唯一方法是返回上一个活动,然后返回转换活动。 The main spinner will appear blank again but the second spinner is updated. 主微调器将再次显示为空白,但第二个微调器已更新。

I want to make it so the second spinner updates the selection as soon as the user selects something from the main spinner. 我要这样做,以便第二个微调器在用户从主微调器中选择某些内容后立即更新选择。 Is there any method I could use do this? 有什么我可以使用的方法吗? Are there any loops involved? 是否有任何循环?

I apologize if my question isn't clear. 如果我的问题不清楚,我深表歉意。 This is my first time asking a question on this website. 这是我第一次在此网站上提问。

Instead of making the OnItemSelectedListener a separate class, make it an inner class of Convert . 不要将OnItemSelectedListener为单独的类,而OnItemSelectedListener其作为Convert的内部类。 Then make your spinner variables members of Convert instead of just local variables within onCreate . 然后,使您的微调器变量成为Convert成员,而不仅仅是onCreate局部变量。 Now your inner class will have direct access to the second spinner thus allowing you to manipulate it from within the onItemSelected method. 现在,您的内部类将可以直接访问第二个微调器,从而使您可以从onItemSelected方法中对其进行操作。

For a non-android explanation of inner class you can refer to the Java Tutorials here . 对于内部类的非Android解释,您可以在此处参考Java教程。

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

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