android - fill fragment edttexts with data from activity -
thx in advance time. driving me crazy, want fill edit texts located layout of fragment called button in main activity , want have edit text filled data of asynctask on main activity.i've tried code when open fragment can't close fragment , empty. please please give solution or directions find solution.
this code of activity:
public class resultadoscopia extends activity implements datos.onfragmentinteractionlistener { imageview foto; button sig; button ant; button adopta; string hld; string postal; string animal; string email; string telf; string edad; string nomanimal; string funciones; string cchld; string neces; string ccanimal; string ccpostal; string nomcont; // progress dialog private progressdialog pdialog; // json parser class jsonparser jsonparser = new jsonparser(); // single product url public string url_product_detials = "http://adoptia.esy.es/androptar.php"; // json node names public static final string tag_hld = "hld"; public static final string tag_postal = "postal"; public static final string tag_animal = "animal"; public static final string tag_success = "success"; public static final string tag_product = "product"; public static final string tag_email = "email"; public static final string tag_tel = "telf"; public static final string tag_edad = "edad"; public static final string tag_nanimal = "nomanimal"; public static final string tag_ncont = "nomcont"; public static final string tag_neces = "neces"; public static final string tag_funciones = "funciones"; public static final string tag_foto = "foto"; public bitmap loadedimage; int bcount = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_resultados1); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } adopta = (button)findviewbyid(r.id.add_fragment); adopta.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //paso 1: obtener la instancia del administrador de fragmentos fragmentmanager fragmentmanager = getfragmentmanager(); //paso 2: crear una nueva transacción fragmenttransaction transaction = fragmentmanager.begintransaction(); //paso 3: crear un nuevo fragmento y añadirlo bundle bundle = new bundle(); string accanimal=ccanimal; string aedad=edad; string aemail=email; string acchld=cchld; string anomanimal=nomanimal; string anomcont=nomcont; string afunciones=funciones; string aneces=neces; string accpostal=ccpostal; string atelf=telf; bundle.putstring("ccanimal",accanimal); bundle.putstring("edad",aedad); bundle.putstring("email",aemail); bundle.putstring("cchld",acchld); bundle.putstring("nomanimal",anomanimal); bundle.putstring("nomcont",anomcont); bundle.putstring("funciones",afunciones); bundle.putstring("neces",aneces); bundle.putstring("ccpostal",accpostal); bundle.putstring("telf",atelf); datos fraginfo = new datos(); fraginfo.setarguments(bundle); transaction.add(r.id.contenedor, fraginfo); transaction.replace(r.id.contenedor, fraginfo); //paso 4: confirmar el cambio transaction.commit(); } }); sig = (button)findviewbyid(r.id.btn_sig); ant = (button)findviewbyid(r.id.btn_ant); bundle extras = getintent().getextras(); hld = extras.getstring(tag_hld); postal = extras.getstring(tag_postal); animal = extras.getstring(tag_animal); foto = (imageview)findviewbyid(r.id.cfoto); // getting complete product details in background thread new getproductdetails().execute(); sig.setonclicklistener(new view.onclicklistener(){ public void onclick(view v){ bcount++; new getproductdetails().execute(); } }); ant.setonclicklistener(new view.onclicklistener(){ public void onclick(view v){ bcount--; new getproductdetails().execute(); } }); } public void irselect(view v){ intent intent = new intent(resultadoscopia.this, select.class); startactivity(intent); } @override public void onfragmentinteraction(uri uri) { } /** * background async task complete product details * */ class getproductdetails extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(resultadoscopia.this); pdialog.setmessage("buscando animales..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * getting product details in background thread * */ protected string doinbackground(string... params) { // updating ui background thread runonuithread(new runnable() { public void run() { // check success tag int success; try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("hld", hld)); params.add(new basicnamevaluepair("postal", postal)); params.add(new basicnamevaluepair("animal", animal)); // getting product details making http request // note product details url use request jsonobject json = jsonparser.makehttprequest( url_product_detials, "get", params); // check log json response log.d("animal", json.tostring()); // json success tag success = json.getint(tag_success); if (success == 1) { // received product details final jsonarray productobj = json .getjsonarray(tag_product); // json array // first product object json array jsonobject product = productobj.getjsonobject(bcount); // display product data in edittext ccanimal=(product.getstring(tag_animal)); edad=(product.getstring(tag_edad)); email=(product.getstring(tag_email)); cchld=(product.getstring(tag_hld)); nomanimal=(product.getstring(tag_nanimal)); nomcont=(product.getstring(tag_ncont)); funciones=(product.getstring(tag_funciones)); neces=(product.getstring(tag_neces)); ccpostal=(product.getstring(tag_postal)); telf=(product.getstring(tag_tel)); url imageurl; try { imageurl = new url("http","adoptia.esy.es",product.getstring(tag_foto)); httpurlconnection conn = (httpurlconnection) imageurl.openconnection(); conn.connect(); loadedimage = bitmapfactory.decodestream(conn.getinputstream()); } catch (ioexception e) { toast.maketext(getapplicationcontext(), "error cargando la imagen: " + e.getmessage(), toast.length_long).show(); e.printstacktrace(); } foto.setimagebitmap(loadedimage); }else{ // product pid not found } } catch (jsonexception e) { e.printstacktrace(); } } }); return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once got details pdialog.dismiss(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_resultados, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
and fragment code:
public class datos extends fragment { edittext email; edittext telf; edittext edad; edittext nomanimal; edittext funciones; edittext cchld; edittext neces; edittext ccanimal; edittext ccpostal; edittext nomcont; private onfragmentinteractionlistener mlistener; public datos() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { bundle bundle = this.getarguments(); string raza = bundle.getstring("ccanimal"); string hld = bundle.getstring("cchld"); string postal = bundle.getstring("ccpostal"); string emilio = bundle.getstring("email"); string tel = bundle.getstring("telf"); string edade = bundle.getstring("edad"); string nomani = bundle.getstring("nomanimal"); string nece = bundle.getstring("neces"); string func = bundle.getstring("funciones"); string nomcon = bundle.getstring("nomcont"); view view = inflater.inflate(r.layout.fragment_datos, container, false); //nuevos parametros para el view del fragmento relativelayout.layoutparams params = new relativelayout.layoutparams( relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent); //nueva regla: el fragmento estara debajo del boton add_fragment params.addrule(relativelayout.above, r.id.add_fragment); //margenes: top:41dp params.setmargins(0,0,0,0); //setear los parametros al view view.setlayoutparams(params); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } ccanimal = (edittext)view.findviewbyid(r.id.canimal); cchld = (edittext)view.findviewbyid(r.id.chdl); ccpostal=(edittext)view.findviewbyid(r.id.cpostal); email = (edittext)view.findviewbyid(r.id.cemail); telf = (edittext)view.findviewbyid(r.id.ctelf); edad = (edittext)view.findviewbyid(r.id.cedad); nomanimal = (edittext)view.findviewbyid(r.id.cnomanimal); nomcont = (edittext)view.findviewbyid(r.id.cnomcont); neces =(edittext)view.findviewbyid(r.id.cneces); funciones =(edittext)view.findviewbyid(r.id.cfunciones); ccanimal.settext(raza); edad.settext(edade); email.settext(emilio); cchld.settext(hld); nomanimal.settext(nomani); nomcont.settext(nomcon); funciones.settext(func); neces.settext(nece); ccpostal.settext(postal); telf.settext(tel); // inflate layout fragment return inflater.inflate(r.layout.fragment_datos, container, false); } // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) { if (mlistener != null) { mlistener.onfragmentinteraction(uri); } } @override public void onattach(activity activity) { super.onattach(activity); try { mlistener = (onfragmentinteractionlistener) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement onfragmentinteractionlistener"); } } @override public void ondetach() { super.ondetach(); mlistener = null; } /** * interface must implemented activities contain * fragment allow interaction in fragment communicated * activity , potentially other fragments contained in * activity. * <p/> * see android training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >communicating other fragments</a> more information. */ public interface onfragmentinteractionlistener { // todo: update argument type , name public void onfragmentinteraction(uri uri); }
Comments
Post a Comment