如何管理两个微调器setOnItemSelectedListener android

时间:2013-11-13 10:49:51

标签: android spinner android-spinner

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

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

我想要做什么:我想在页面上绘制一个图形,微调器1将会显示不同的图形,旋转器2将绘制不同的图形页面加载。

任何帮助都会得到赞赏。

2 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分: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);
    }