java - Activity send data to Fragment with interface -
i want activity send fragment. have error see below photo. activity fragment data send. want when listview onclick fragment il value temp data. fragment want see humidity value ,temp value when setonitemclicklistener
mainactivity.java
public class mainactivity extends appcompatactivity { private string text ; private textview textview, textview2,textview3,textview4; private searchview searchview; private listview listview ; private string[] il={"adana", "adıyaman", "afyon", "ağrı", "amasya", "ankara", "antalya", "artvin", "aydın", "balıkesir", "bilecik", "bingöl", "bitlis", "bolu", "burdur", "bursa", "Çanakkale", "Çankırı", "Çorum", "denizli", "diyarbakır", "edirne", "elazığ", "erzincan", "erzurum", "eskişehir", "gaziantep", "giresun", "gümüşhane", "hakkari", "hatay", "isparta", "mersin", "İstanbul", "İzmir", "kars", "kastamonu", "kayseri", "kırklareli", "kırşehir", "kocaeli", "konya", "kütahya", "malatya", "manisa", "kahramanmaraş", "mardin", "muğla", "muş", "nevşehir", "niğde", "ordu", "rize", "sakarya", "samsun", "siirt", "sinop", "sivas", "tekirdağ", "tokat", "trabzon", "tunceli", "Şanlıurfa", "uşak", "van", "yozgat", "zonguldak", "aksaray", "bayburt", "karaman", "kırıkkale", "batman", "Şırnak", "bartın", "ardahan", "iğdır", "yalova", "karabük", "kilis", "osmaniye", "düzce"}; private arrayadapter<string> arrayadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); searchview= (searchview) findviewbyid(r.id.searchview); listview= (listview) findviewbyid(r.id.listview); final senddata senddata = (senddata) this; fragmenta fragmenta = new fragmenta() ; fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.add(r.id.framelayout,fragmenta,"fraga"); transaction.commit(); arrayadapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,android.r.id.text1,il); listview.setadapter(arrayadapter); searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { arrayadapter.getfilter().filter(newtext); return false; } }); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, final int position, long id) { text = (string) listview.getitematposition(position); senddata.gonder(text); } }); }
}
fragmenta.java
public class fragmenta extends fragment implements senddata { private textview textview, textview2,textview3,textview4; view view ; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_a,container,false) ; textview4= (textview) view. findviewbyid(r.id.textisım); textview3= (textview) view.findviewbyid(r.id.textview3); textview2= (textview) view.findviewbyid(r.id.textview2); textview= (textview) view. findviewbyid(r.id.textview); return view; } @override public void gonder(string isim) { factory.getinstance().havamodel(isim,appid_key).enqueue(new callback<havamodel>() { @override public void onresponse(call<havamodel> call, response<havamodel> response) { textview.settext(float.tostring((float) (response.body().main.temp-273.15))); textview2.settext(float.tostring(response.body().coord.lon)); textview3.settext(integer.tostring(response.body().main.humidity)+" %"); textview4.settext(response.body().name); } @override public void onfailure(call<havamodel> call, throwable t) { } }); }
senddata.java
public interface senddata { public void gonder(string isim) ; }
thank advice important me hope me
you have set interface calling activity.
your mainactivity should this.
public class mainactivity extends appcompatactivity { private string text ; private textview textview, textview2,textview3,textview4; private searchview searchview; private listview listview ; private senddata senddata; // initialize globally private string[] il={"adana", "adıyaman", "afyon", "ağrı", "amasya", "ankara", "antalya", "artvin", "aydın", "balıkesir", "bilecik", "bingöl", "bitlis", "bolu", "burdur", "bursa", "Çanakkale", "Çankırı", "Çorum", "denizli", "diyarbakır", "edirne", "elazığ", "erzincan", "erzurum", "eskişehir", "gaziantep", "giresun", "gümüşhane", "hakkari", "hatay", "isparta", "mersin", "İstanbul", "İzmir", "kars", "kastamonu", "kayseri", "kırklareli", "kırşehir", "kocaeli", "konya", "kütahya", "malatya", "manisa", "kahramanmaraş", "mardin", "muğla", "muş", "nevşehir", "niğde", "ordu", "rize", "sakarya", "samsun", "siirt", "sinop", "sivas", "tekirdağ", "tokat", "trabzon", "tunceli", "Şanlıurfa", "uşak", "van", "yozgat", "zonguldak", "aksaray", "bayburt", "karaman", "kırıkkale", "batman", "Şırnak", "bartın", "ardahan", "iğdır", "yalova", "karabük", "kilis", "osmaniye", "düzce"}; private arrayadapter<string> arrayadapter; public void setsenddata(senddata senddata){ //create setter interface this.senddata = senddata; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); searchview= (searchview) findviewbyid(r.id.searchview); listview= (listview) findviewbyid(r.id.listview); fragmenta fragmenta = new fragmenta() ; fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.add(r.id.framelayout,fragmenta,"fraga"); transaction.commit(); arrayadapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,android.r.id.text1,il); listview.setadapter(arrayadapter); searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { arrayadapter.getfilter().filter(newtext); return false; } }); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, final int position, long id) { text = (string) listview.getitematposition(position); senddata.gonder(text); } }); }
your fragment should this.
public class fragmenta extends fragment implements senddata { private textview textview, textview2,textview3,textview4; view view ; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_a,container,false) ; textview4= (textview) view. findviewbyid(r.id.textisım); textview3= (textview) view.findviewbyid(r.id.textview3); textview2= (textview) view.findviewbyid(r.id.textview2); textview= (textview) view. findviewbyid(r.id.textview); if(getactivity() instanceof mainactivity) (mainactivity)getactivity().setsenddata(this); //set interface in mainactivity return view; } @override public void gonder(string isim) { factory.getinstance().havamodel(isim,appid_key).enqueue(new callback<havamodel>() { @override public void onresponse(call<havamodel> call, response<havamodel> response) { textview.settext(float.tostring((float) (response.body().main.temp-273.15))); textview2.settext(float.tostring(response.body().coord.lon)); textview3.settext(integer.tostring(response.body().main.humidity)+" %"); textview4.settext(response.body().name); } @override public void onfailure(call<havamodel> call, throwable t) { } }); }
hope helps:)
Comments
Post a Comment