基于多个onItemSelectedListeners执行任务

时间:2017-09-19 02:16:25

标签: android spinner onitemselectedlistener

我使用三个微调器来获取用户的输入,然后根据这些信息,我将绘制一些数据。但我似乎无法弄清楚如何做到这一点。我有以下代码(提取以显示重要位):

final GraphView graph = (GraphView) findViewById(R.id.graph);
Spinner selectGraphSpinner = (Spinner) findViewById(R.id.graphSpinner);
Spinner selectUserSpinner = (Spinner) findViewById(R.id.userSpinner);
Spinner selectTimeSpinner = (Spinner) findViewById(R.id.timeSpinner);
    String[] items = new String[]{"Please Select a User", "04950f4ae53f80", "another user"};

    // Create ArrayAdapters for Spinners
ArrayAdapter<CharSequence> selectGraphAdapter = ArrayAdapter
            .createFromResource(this, R.array.Graphs,
                    android.R.layout.simple_spinner_item);
ArrayAdapter<String> userSpinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, items);
ArrayAdapter<CharSequence> spinTimeAdapter = ArrayAdapter
            .createFromResource(this, R.array.TimeIncrements,
                    android.R.layout.simple_spinner_item);  
selectGraphSpinner.setAdapter(selectGraphAdapter);
selectUserSpinner.setAdapter(userSpinnerAdapter);
selectTimeSpinner.setAdapter(spinTimeAdapter);  

onItemSelectedListeners x 3重复其他微调器:

selectUserSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            itemOne = parent.getItemAtPosition(0).toString();
        }

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

到目前为止的图形逻辑:

while (itemOne != null) {
        if (itemOne.equalsIgnoreCase("04950f4ae53f80")) {
            if (itemTwo.equalsIgnoreCase("Estimated vs Actual Time")) {
                if (itemThree.equalsIgnoreCase("Day")) {
                    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(getData(Data, "Day"));

                    graph.addSeries(series);

                } else {

                }
            } else {

            }

        } else {

        }
    }

我知道图形工作正如我尝试使用一个微调器,但无法弄清楚为什么监听器不保存变量(我声明全局 - 静态字符串itemOne等) 谢谢 编辑:微调器包含多个具有不同初始字符串的字符串,但提到了使用过的字符串,

1 个答案:

答案 0 :(得分:0)

我设法通过将每个微调器中的单击项保存到全局函数/变量来实现此目的。然后,这允许我运行逻辑检查,根据具体情况返回true或false。然后我将图形方法放在监听器中,以便在逻辑检查返回true时实现它