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
Solution 1:
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 = newArrayList<String>();
StringLaptops[] = {"Dell","Hp","Apple"};
ListView lstView;
ArrayAdapter<String> adapter,listadapter;
publicvoidonCreate(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 = newArrayAdapter<String> (this, android.R.layout.simple_spinner_item,path);
listadapter = newArrayAdapter<String> (this, android.R.layout.simple_spinner_item,list);
lstView.setAdapter(listadapter);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stubString s = spinner.getItemAtPosition(arg2).toString();
list.add(s);
listadapter.notifyDataSetChanged();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Solution 2:
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();
Solution 3:
Add notifyDataSetChanged() to adapter like below....
spinner.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid 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();
}
});
Solution 4:
Use Spinner onitemSelectedListener
instead of onItemClickListener
Try the following :
spinner.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code hereArrayAdapter<String> lstAdapter = newArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
lstView.notifyDataSetChanged();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Post a Comment for "Android - Updating List View On Spinner Selection"