android - Accessing component within layout inflater -
room b lamp 2 turn on status turn green on room lamp 2 status
i created mainlayout using layoutinflater layout. add more control dynamically. can create many sub layout dynamically within mainlayout. problem how access particular textview in current sublayout operation. eg if press button on lamp2 in room b, status next turn green or red.
this mainactivity load layouts
public class mainactivity extends activity { button buttonadd; linearlayout container; sqlitedatabase db1; string sa; string sb; textview txtsta1; textview txtsta2; //textview txtsta1; //textview txtsta2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); container = (linearlayout) findviewbyid(r.id.container); createdb(); allcontrol(); layouttransition transition = new layouttransition(); container.setlayouttransition(transition); } public void addlayoutcontrol(string a, string room, string b, string c, final int d) { layoutinflater layoutinflater = (layoutinflater) getbasecontext() .getsystemservice(context.layout_inflater_service); final view addview = layoutinflater.inflate(r.layout.row, null); final textview txtip = (textview) addview.findviewbyid(r.id.textip); final textview txtroom = (textview) addview.findviewbyid(r.id.textroom); final textview txts1 = (textview) addview.findviewbyid(r.id.tvs1); final textview txts2 = (textview) addview.findviewbyid(r.id.tvs2); txtsta1 = (textview) addview.findviewbyid(r.id.textsta1); txtsta2 = (textview) addview.findviewbyid(r.id.textsta2); txtip.settext(a); txtroom.settext(room); txts1.settext(b); txts2.settext(c); new onstatusswitch().execute(a); button buttonon1 = (button) addview.findviewbyid(r.id.on1); buttonon1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { new onswitch1on2g().execute(txtip.gettext().tostring(),"b1on"); } }); button buttonon2 = (button) addview.findviewbyid(r.id.on2); buttonon2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { new onswitch1on2g().execute(txtip.gettext().tostring(),"b2on"); } }); button buttonoff1 = (button) addview.findviewbyid(r.id.off1); buttonoff1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { new onswitch1off2g().execute(txtip.gettext().tostring(),"b1off"); } }); button buttonoff2 = (button) addview.findviewbyid(r.id.off2); buttonoff2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { new onswitch1off2g().execute(txtip.gettext().tostring(),"b2off"); } }); container.addview(addview, 0); } public void calladd(view v) { intent intent = new intent(this, addnode.class); startactivity(intent); } public void createdb() { db1 = this.openorcreatedatabase("dbcontrol", mode_private, null); string sqlcreate = "create table if not exists node" + "(type varchar, ip varchar,location varchar,s1 varchar, s2 varchar);"; db1.execsql(sqlcreate); } public void allcontrol() { string sqlsearch = "select * node"; string sip = ""; cursor c = db1.rawquery(sqlsearch, null); if (c.getcount() > 0) { c.movetofirst(); //while (!c.()){ (int = 0; < c.getcount(); i++) { sip = c.getstring(c.getcolumnindex("ip")); string sloc = c.getstring(c.getcolumnindex("location")); string ss1 = c.getstring(c.getcolumnindex("s1")); string ss2 = c.getstring(c.getcolumnindex("s2")); addlayoutcontrol(sip, sloc, ss1, ss2,i); c.movetonext(); } } } class onswitch1on2g extends asynctask<string, string, string> { string serverip,cond; @override protected void onpreexecute() { } @override protected string doinbackground(string... params) { serverip = params[0]; cond = params[1]; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://" + serverip + ":88/operation?"); try { // add data list<basicnamevaluepair> namevaluepairs = new arraylist<basicnamevaluepair>(1); namevaluepairs.add(new basicnamevaluepair("ope", cond)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); } catch (exception e) { // todo: handle exception } return null; } protected void onpostexecute(string ab) { // new onstatusswitch().execute(); //toast.maketext(getapplicationcontext(), "success "+serverip+ " button "+cond , toast.length_short).show(); new onstatusswitch().execute(serverip); } } class onswitch1off2g extends asynctask<string, string, string> { string serverip,cond; @override protected void onpreexecute() { } @override protected string doinbackground(string... params) { serverip = params[0]; cond = params[1]; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://" + serverip + ":88/operation?"); try { // add data list<basicnamevaluepair> namevaluepairs = new arraylist<basicnamevaluepair>(1); namevaluepairs.add(new basicnamevaluepair("ope", cond)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); } catch (exception e) { // todo: handle exception } return null; } protected void onpostexecute(string ab) { // new onstatusswitch().execute(); new onstatusswitch().execute(serverip); //toast.maketext(getapplicationcontext(), "success "+serverip+ " button "+cond , toast.length_short).show(); } } class onstatusswitch extends asynctask<string, string, string> { jsonparser jsonparser = new jsonparser(); jsonobject jobj = null; string pina; string pinb; @override protected string doinbackground(string... params) { try { string serverip = params[0]; string url = "http://" + serverip + ":88/getstatus"; jobj = jsonparser.makehttprequest(url); pina = jobj.getstring("pin1"); pinb = jobj.getstring("pin2"); runonuithread(new runnable() { public void run() { if (pina.equals("0")) { txtsta1.setbackgroundcolor(color.green); }else{ txtsta1.setbackgroundcolor(color.red); } if (pinb.equals("0")) { txtsta2.setbackgroundcolor(color.green); }else{ txtsta2.setbackgroundcolor(color.red); } } }); return pina+pinb; } catch (exception e2) { } return null; } protected void onpostexecute(string ab) { sa = ab.substring(0,1); sb = ab.substring(1); } }
this bugging me..need solve problem..not quite familiar layout control.
first, recommend 1 togglebutton
or switch
instead of 2 individual buttons, if "bind" state of textview button, can create custom clicklistener class.
class mycheckchangedlistener implements compoundbutton.oncheckedchangelistener { private textview tv; public mycheckchangedlistener(textview tv) { this.tv = tv; } public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo: if (ischecked) { network operation } tv.setbackgroundcolor(ischecked ? color.green : color.red); } };
to use, so
textview txtstatus = (textview) findviewbyid(r.id.status); togglebutton toggle = (togglebutton) findviewbyid(r.id.togglebutton); toggle.setoncheckedchangelistener(new mycheckchangedlistener(txtstatus));
though, nice thing togglebutton don't need textview, can see "status" looking @ state of button itself.
Comments
Post a Comment