android - Update Listview in Fragment , based on activity result . No result found anywhere -
i have activity , fragment in activity have button in action bar ,on button click dialog box popup , in have listview. if select item list , data goes fragment , , based on data activity fragment listview should shown
e.g. if select technology , technology id =1 should pass in query parameter , based on fragment listview should appear. everytime select different item list fragment listview should updated.
but problem not getting data in fragment , list not getting updated
mainactivity.java
@override public boolean onprepareoptionsmenu(menu menu) { menuitem actionviewitem = menu.finditem(r.id.miactionbutton); // retrieve action-view menu view v = menuitemcompat.getactionview(actionviewitem); // find button within action-view button b = (button) v.findviewbyid(r.id.btncustomaction); // handle button click here b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub showdialog(custom_dialog_id); } }); return super.onprepareoptionsmenu(menu); } @override public dialog oncreatedialog(int id) { dialog dialog = null; switch (id) { case custom_dialog_id: dialog = new dialog(this); dialog.setcontentview(r.layout.dialoglayout); dialog.settitle("select category"); dialog.setcancelable(true); dialog.setcanceledontouchoutside(true); dialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface dialog) { } }); dialog.setondismisslistener(new ondismisslistener() { @override public void ondismiss(dialoginterface dialog) { // todo auto-generated method stub } }); // prepare listview in dialog dialog_listview = (listview) dialog.findviewbyid(r.id.dialoglist); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listcontent); dialog_listview.setadapter(adapter); dialog_listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub // = (int) parent.getitemidatposition(position); category_name = (string) parent.getitematposition(position); log.d("category name", category_name); if(category_name.equals("a")) { cat_id=1; } if(category_name.equals("b")) { cat_id=2; } if(category_name.equals("c")) { cat_id=3; } if(category_name.equals("d")) { cat_id=4; } if(category_name.equals("e")) { cat_id=5; } if(category_name.equals("f")) { cat_id=6; } log.d("id",""+cat_id); dismissdialog(custom_dialog_id); } }); break; } return dialog; } public int getmydata() { return cat_id; }
fragment.java :
public string param; public string url; public int cat_id; mainactivity activity = (mainactivity) getactivity(); int mydatafromactivity = activity.getmydata(); cat_id = mydatafromactivity; log.d("category_id", "" + cat_id); map<string, string> map1 = new hashmap<string, string>(); param = "?cat_id=" + cat_id; //url fake , original url working fine. url = "http://www.abbcd.com/xyz/webservices/data/getalldata.php" + param; map1.put("url", url); new multipartrequester(mainactivity, map1, netutils.requestcode.get_all_data, this).execute();
updated part :
@override public void updatefragmentlist(int catid) { // todo auto-generated method stub param = "?cat_id=" + catid; url = "http://www.abbcd.com/xyz/webservices/data/getalldata.php"+ param; map<string, string> map1 = new hashmap<string, string>(); map1.put("url", url); new multipartrequester(mainactivity, map1, netutils.get_data, this) .execute(); }
the approach communication between activity
, fragment
listeners
. when want update fragment, use listener , update fragment. create interface in mainactivity
listener
public interface fragmentlistener { void updatefragmentlist(int catid); }
implement in fragment
public class myfragment extends fragment implements mainactivity.fragmentlistener
then in onactivitycreated
of fragment
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); ((mainactivity) getactivity()).setfragmentlistener(this); }
mainactivity
public void setfragmentlistener(fragmentlistener listener) { this.fragmentlistener = listener; } public void senddatatofragment(int catid) { this.fragmentlistener.updatefragmentlist(catid); }
hope helps.
update
sending data fragment using this.fragmentlistener.updatefragmentlist(catid);
, create method in fragment
``
public void updatefragmentlist(int catid){ //here update listview cat id }
Comments
Post a Comment