java - EditText and Reflection -
my problem little particular.
i show forth in code:
import android.app.activity; import android.content.context; import android.widget.edittext; import java.lang.reflect.field; import java.util.hashmap; public class myactivity extends activity { public hashmap initedittext(context context){ hashmap list = new hashmap(); field[] fields = context.getclass().getdeclaredfields(); (field field : fields){ if(field.getname().startswith("e_")){ string word = field.getname().substring(2); string firscaract = word.substring(0, 1).touppercase(); string key = field.getname().substring(3); try{ class<?> cl = field.gettype(); // value of member in object: (object instance of myclass) //object o = field.get(c); if (edittext.class.isassignablefrom(cl)) { edittext value = (edittext) field.get(context); list.put(firscaract + key, value.gettext().tostring()); } } catch (illegalaccessexception e){ e.printstacktrace(); } } } return list; } }`
this class, myactivity, serve me mother class
import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import java.lang.reflect.field; import java.util.hashmap; import java.util.iterator; public class testactivity extends myactivity { public edittext e_prenom = null; private edittext e_nom = null; private textview t_prenom = null; private textview t_nom = null; private button validate = null; private textview display = null; private hashmap list; string text2 = ""; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_testt); e_prenom = (edittext) findviewbyid(r.id.edit_register_prenom); e_nom = (edittext) findviewbyid(r.id.edit_register_nom); t_prenom = (textview) findviewbyid(r.id.text_register_prenom); t_nom = (textview) findviewbyid(r.id.text_register_nom); validate = (button) findviewbyid(r.id.valider_register); display = (textview) findviewbyid(r.id.affiche); list = new hashmap(); e_prenom.settext("du con"); e_nom.settext("du puis"); list = initedittext(this); iterator = liste.keyset().iterator(); stringbuilder builder = new stringbuilder(); while(it.hasnext()){ object key = it.next(); object value = liste.get(key); builder.append(string.valueof(key) + "_" + string.valueof(value) + "\n"); } text2 = builder.tostring(); validate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { display.settext(text2); } }); } }
testactivity, child class inherits myactivity.
in myactivity initedittext method allows recover fields starting "e_" (those more precisely edittext) , storing names , values in hashmap.
in testactvity, have 2 edittext "e_prenom" , "e_nom". calling initedittext stock value in hashmap traveled , clicking on "validate" content of hahmap ("list") displayed in textview ("display").
so expect have result textview "prenom_du con" "nom_du puis".
but "prenom_du con" displayed.
anybody know why ?
after several investigations , tests noticed line
field[] fields = context.getclass().getdeclaredfields();
all fields recovered
but here
if (edittext.class.isassignablefrom(cl)) { edittext value = (edittext) field.get(context); list.put(firscaract + key, value.gettext().tostring()); }
only 1 of fields (edittext) has value retrieved.
is there property of view (edittext accurate in case) not know , cause ? comes property of field.get (object object)?
looks problem modifiers (public,private...):
public edittext e_prenom = null; private edittext e_nom = null;
as can see 1 public, , other private.
to access private field, make accessible:
field.setaccessible(true);
so overall, it'll like:
public hashmap initedittext(context context){ hashmap list = new hashmap(); field[] fields = context.getclass().getdeclaredfields(); (field field : fields){ field.setaccessible(true); //add line here if(field.getname().startswith("e_")){ string word = field.getname().substring(2); string firscaract = word.substring(0, 1).touppercase();
Comments
Post a Comment