java - How to add values to a ListView in other activity? -
in app have alertdialog contains button named "add favorites", when click on button, want send values listview in activity, values i'm trying add retrieved arraylist, can help?
this have:
.setnegativebutton("favoritos",new dialoginterface.onclicklistener(){ public void onclick(dialoginterface dialog,int which){ int position=(int)marker.gettag(); final listview listreprfav=(listview)findviewbyid(r.id.listafavrepresentantes); listadapter adapterreprfav=new simpleadapter( (mapsactivity.this), listarepresentantes, r.layout.list_item_representantes_fav, new string[]{listaoportunidades.get(position).get("designacao"),listaoportunidades.get(position).get("morada")}, new int[]{r.id.designacaoreprfav,r.id.cidade}); listreprfav.setadapter(adapterreprfav); } }
first create global class keep context of listview activity.
public class global extends application { context context; } then need create public method in activity contains listview:
public class listviewactivity extends activity { public list<string> listitems; public arrayadapter<string> adapter; @override protected void oncreate(bundle savedinstancestate) { .... global.context = this; //set context in global. listview listview = (listview) findviewbyid(r.id.listview1); adapter = new arrayadapter<string> (this, android.r.layout.simple_list_item_1, android.r.id.text1, listitems); listview.setadapter(adapter); } //this method add new string listitems array , //notifies adapter changes. public void additemandrefreshlistview(string[] newitems){ for(int = 0; i<newitems.length; i++){ listitems.add(newitem); } adapter.notifydatasetchanged(); } } after that, in onclick of dialog button:
.setnegativebutton("favoritos",new dialoginterface.onclicklistener(){ public void onclick(dialoginterface dialog,int which){ if(global.context != null){ ((listviewactivity) global.context).additemandrefreshlistview(values); } } }
Comments
Post a Comment