Android: Create a new intent with a button from a GridView (GridView with Navigation buttons) -
Android: Create a new intent with a button from a GridView (GridView with Navigation buttons) -
i have created class of type baseadapter populated buttons - when click on button want load new intent. has proven hard on 2 levels:
you cannot associate event button (one creates new intent) within adapter. why send buttons array adapter (this solution works, messy)
even though buttons created within same activity - cannot create new intent activity. exeption great have not gotten try...catch statement work.
i have tried reflection, creating buttons within activity , passing them through, passing context (to phone call context.startintent(...))
my question: can show me how create buttonadapter each button creates new intent - of same type original activity?
update: here code because getting answers people think struggling onclicklisteners:
public class buttonadapter extends baseadapter { private context _context; private button[] _button; public buttonadapter(context c, button[] buttons) { _context = c; _button = buttons; } // total number of things contained within adapter public int getcount() { homecoming _button.length; } // require structure, not used in code. public object getitem(int position) { homecoming _button[position]; } // require structure, not used in code. can // used id of item in adapter // manual control. public long getitemid(int position) { homecoming position; } public view getview(int position, view convertview, viewgroup parent) { _button[position].setid(position); homecoming _button[position]; } }
--------------- activity:
public class mainactivity extends activity { private gridview _gv; private textview _heading; private buttonadapter _adapter; public void loadactivity(string heading) { seek { itent intent = new intent(getapplicationcontext(), mainactivity.class); intent.putextra("level", "nextpage"); intent.putextra("heading", heading); startactivity(intent); } grab (exception ex) { toast.maketext(getapplicationcontext(), string.format("error loadactivity: %s", ex.getmessage()), toast.length_short).show(); } } private void createbuttonsadapter(button _button[]) { _buttonadapter = new buttonadapter(getapplicationcontext(), _button); _gv.setadapter(_adapter); } private void setupbuttons() { button[] _buttons = new button[2]; string names[] = new string[]{"button1","button2"}; (int = 0; < 2; i++) { _buttons[i] = new button(this); _buttons[i].settext(names[i]); _buttons[i].settag(names[i]); _buttons[i].setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { seek { loadactivity(((button)arg0).gettag().tostring()); } catch(exception ex) { toast.maketext(getapplicationcontext(), string.format("error button.onclick: %s", ex.getmessage()), toast.length_short).show(); } } }); } createbuttonsadapter(_buttons); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); _gv = (gridview)findviewbyid(r.id.gridview); _heading = (textview)findviewbyid(r.id.tv_heading); bundle params = getintent().getextras(); if (params == null) { setupbuttons(); } else if (params.containskey("level")) { _heading.settext(params.getstring("heading")); if (params.getstring("level").equals("nextpage")) { //code not here yet } else if (params.getstring("level").equals("chapters")) { //future code } } } }
excuse bold , caps have had plenty silly answers warrent this: i have tried putting onclicklistener within gridview , not work either cannout load activity class outside activity, if pass context parameter. why have resorted method, bombs android, though have seek grab statements.
please seek give me solution in form of correction code, other code, or tutorial achieves want here. know how button adapter properly, deed of loading intent has forced me implement way.
i suggest following,
using mutual onclick listner buttons in grid view set tag butons in getview func. of adapter. use tag object decide on intent fire onclick listener.i hope helps..
android gridview android-intent navigation
Comments
Post a Comment