java - List view activity is not working properly in switch case inside spinner class -
here booklist.java spinner class maintains 2 occurrences 1 book , author , both have own list view class. through switching cases inside onitemselected() method intent accessed. problem i'm facing first list view applied intent book can't accessed can access author intent. helpful if me editing code. here goes java file.
public class booklist extends activity implements onitemselectedlistener { spinner spinner; //private static final string[]paths = {"book", "author"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_booklist); textview t =(textview)findviewbyid(r.id.t1) ; spinner = (spinner)findviewbyid(r.id.spinner); //arrayadapter<string>adapter = new arrayadapter<string>(booklist.this, //android.r.layout.simple_spinner_item,paths); arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this, r.array.blist_array, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); adapter.notifydatasetchanged(); spinner.setonitemselectedlistener(this); } public void onitemselected(adapterview<?> parent, view view, int position,long id) { final intent intent; switch (position) { case 1: intent = new intent(booklist.this, booklistview.class); startactivity(intent); break; case 2: intent = new intent(booklist.this, booklistauthorview.class); startactivity(intent); break; } } public void onnothingselected(adapterview<?> arg0) { toast.maketext(this, "please choose field", toast.length_short).show(); } }
the item positions starts 0 not 1 use this
and 0 position automatically trigger first time open activity create spinner array spinner
string arr ={"select choice","book","author"}; switch (position) { case 1: intent = new intent(booklist.this, booklistview.class); startactivity(intent); break; case 2: intent = new intent(booklist.this, booklistauthorview.class); startactivity(intent); break; } plus don't have call `adapter.notifydatasetchanged();'
Comments
Post a Comment