简体   繁体   English

如何管理两个微调器setOnItemSelectedListener android

[英]how to manage two spinner setOnItemSelectedListener android

I used two spinners in same fragment I want when the first spinner will complete its task then only the setOnItemSelectedListener will call for the second spinner. 当第一个微调器将完成其任务时,我在同一片段中使用了两个微调器,然后只有setOnItemSelectedListener会调用第二个微调器。

Issue: But the issue is setOnItemSelectedListener is called on same time for both the spinners.How can I manage that. 问题:但是问题是两个微调器同时调用setOnItemSelectedListener,我该如何处理。 :( :(

What I want to do: I want to draw a graph on page load spinner 1 will darw a different graph and spinner 2 will draw a different graphon page load. 我想做的事情:我想在页面加载时绘制图形,微调器1将显示不同的图,微调器2将在页面加载时绘制不同的图。

Any help will be Appreciated. 任何帮助将不胜感激。

Why do you have same setOnItemSelectedListener for both the spinners. 为什么两个微调器都具有相同的setOnItemSelectedListener。

Have structure like this: 具有这样的结构:

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
    }
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);

That is my two spinner that I calle d on onActivityCreated. 那是我在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; 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; AppGlobals.getInstance()。checkSpinner = 1; } }

But the before completing the first spinner functionality the second spinner called. 但是在完成第一个微调器功能之前,第二个微调器调用了。 I have but the int value so that when both the spinner's called the int value will tell the difference. 我只有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);
    }

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

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