user2113484 user2113484 - 1 year ago 41
Android Question

Android - Updating list view on spinner selection

I am trying to update a list view, when I select on an item in spinner, the code works but no output is generated. I cannot figure out the bug.On every click on the item in spinner I want to view different list.

Spinner spinner;
String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
String Laptops[] = {"Dell","Hp","Apple"};
ListView lstView;


lstView = (ListView) findViewById(R.id.listView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
//lstView.refreshDrawableState();

}
});

Answer Source

Use OnItemSelectedListener and use a ArrayList<String>. Use the same for your adapter. Add items to arraylist and call notifyDataSetChanged on your adapter to refresh lsitview.

Also there is no need to initialize your adapter in the spinner selection listener.

Modify the below according to your requirement

 Spinner spinner;
        String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
        ArrayList<String> list = new ArrayList<String>();
        String Laptops[] = {"Dell","Hp","Apple"};
        ListView lstView;
        ArrayAdapter<String> adapter,listadapter;
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
                lstView = (ListView) findViewById(R.id.listView1);
                for(int i=0;i<Laptops.length;i++)
                {
                    list.add(Laptops[i]);
                }
                adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,path);
                listadapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,list);
                lstView.setAdapter(listadapter);
                spinner = (Spinner) findViewById(R.id.spinner1);
                spinner.setAdapter(adapter);
                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        String s = spinner.getItemAtPosition(arg2).toString();
                        list.add(s);
                        listadapter.notifyDataSetChanged();
                    }

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

                    }
                });

            }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download