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 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
}
});
}
Use notifying that data set has been changed instead of setting a new adapter.
// Inside Event callback
path = Laptops; // This is how you change data set
lstView.notifyDataSetChanged();
Add notifyDataSetChanged() to adapter like below....
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);
lstAdapter.notifyDataSetChanged();
//lstView.refreshDrawableState();
}
});
Use Spinner onitemSelectedListener
instead of onItemClickListener
Try the following :
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
lstView.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});