开发者

Android Dynamic Spinner Update

I'm working with Android and Spinners and I need some help. I have a class that creates two spinners and a button. The first spinner is for my category, my second is for my sub-category. What I am trying to do is dynamically update the second spinner (spinner2). I've been trying to use adapter2.clear() but that crashes android, with an error "unable to start activity componentinfo unsupported operation"

Here is my code:

public class MyClass extends MyBaseClass
{
    int category;
    int sub_category;
    ArrayAdapter<String> adapter2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quizes);

        //CATEGORY INFO
        final String[] items1 = new String[] {"One", "Two", "Three"};
        final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, items1);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);

        //SUBCATEGORY INFO
        final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
        final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        adapter2 = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, items2);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.button1);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // do something when the button is clicked



                startActivity(new Intent(MyClass.this, GoToOtherClass.class));
              } 
        });


        //SELECTOR CONTROL FOR SPINNER1 {CATEGORY}
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                MyClass.this.category = spinner1.getSelectedItemPosition();

        //OTHER STUFF


            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        //SELECTOR CONTROL FOR SPINNER2 {SUB-CATEGORY}
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> ar开发者_Python百科g0, View arg1, int arg2, long arg3) {

                MyClass.this.sub_category = spinner2.getSelectedItemPosition();

        //OTHER STUFF

            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }
}

I understand about the .clear()/.add() methods, but anytime I try clear()/add() my program crashes, what do I need to do to fix things so I can change the spinner2 contents for my sub-category list? Any advice would help, as I have spent hours doing things such as:

Object t=adapter2.getitem(0); spinner2.remove((String) t);

or adapter2.clear() and a few other tricks and I have no further ideas left. I am still learning android. I've tried looking at some other posts here on stackoverflow and google but was not sure how to get their ideas working.


After you change the contents of the second Spinner, you need to call adapter2.notifyDataSetChanged(). Without that call, the UI won't update with the new contents of the Spinner, and you could also have problems referencing things that don't exist anymore.


Try this Code..

public class MainActivity extends Activity {

Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    l1=new ArrayList<String>();

    l1.add("A");
    l1.add("B");

    sp1= (Spinner) findViewById(R.id.spinner1);
    sp2= (Spinner) findViewById(R.id.spinner2);

    adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
    adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp1.setAdapter(adp1);

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            pos=arg2;
            add();

        }

        private void add() {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();

            switch(pos)
            {
            case 0:
                l2= new ArrayList<String>();                    
                l2.add("A 1");
                l2.add("A 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            case 1:
                l2= new ArrayList<String>();                    
                l2.add("B 1");
                l2.add("B 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            }

        }

        private void select() {
            // TODO Auto-generated method stub

            sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();

                }

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

                }
            });

        }

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

        }
    });
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜