0

当第一个微调器完成其任务时,我在我想要的同一片段中使用了两个微调器,然后只有 setOnItemSelectedListener 将调用第二个微调器。

问题:但问题是两个微调器同时调用 setOnItemSelectedListener。我该如何管理。:(

我想要做的:我想在页面加载上绘制一个图表,微调器 1 将绘制一个不同的图表,微调器 2 将在页面加载上绘制一个不同的图表。

任何帮助将不胜感激。

4

2 回答 2

0

为什么两个微调器都有相同的 setOnItemSelectedListener 。

有这样的结构:

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {

        func1();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
    }
});



spinner2.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        func2();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
    }
});



private void func1()
{
//do task of spinner1
}
    private void func2()
    {
    //your task for second spinner
    }
于 2013-11-13T10:54:55.117 回答
0
spinnerLeft = (Spinner) getActivity().findViewById(R.id.spinner_left);
    dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLeft.setAdapter(dataAdapter);

spinerRight = (Spinner) getActivity().findViewById(R.id.spinner_right);
    rightSpinnerAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
    rightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinerRight.setAdapter(rightSpinnerAdapter);

那是我在 onActivityCreated 上调用的两个微调器。

spinerRight.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long arg3) { 
// here I called a method to download a chart and showing below 1st spinner

AppGlobals.getInstance().checkSpinner = 1; }

private void addListenerOnSpinnerLeftItemSelection() {
    spinnerLeft.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long arg3) {
 //here I m called the same method to download and show chart below 2nd spinner 

AppGlobals.getInstance().checkSpinner = 1; }

但是在完成第一个微调器功能之前,第二个微调器调用了。我只有 int 值,所以当两个微调器都被调用时,int 值会告诉你不同之处。

if (AppGlobals.getInstance().checkSpinner == 1) {
        chartArea = (LinearLayout) getActivity().findViewById(R.id.chart);
    } else if (AppGlobals.getInstance().checkSpinner == 0) {
        chartArea = (LinearLayout) getActivity().findViewById(R.id.pie_chart);
    }
于 2013-11-13T11:48:59.110 回答