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();
}
});
Use
OnItemSelectedListener
and use aArrayList<String>
. Use the same for your adapter. Add items to arraylist and callnotifyDataSetChanged
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
Add notifyDataSetChanged() to adapter like below....
Use Spinner
onitemSelectedListener
instead ofonItemClickListener
Try the following :
Use notifying that data set has been changed instead of setting a new adapter.