Room b lamp 2 turn on but status turn green on room a lamp 2 status
I created my mainlayout using Layoutinflater from another layout. This will add more control dynamically. I can create as many sub layout dynamically within my mainlayout. The problem is that how to access particular textview in current sublayout operation. Eg if I press button ON from lamp2 in Room b, only status next to it will turn green or red.
This is the my mainactivity that load my 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 * FROM node";
String sIP = "";
Cursor c = db1.rawQuery(sqlSearch, null);
if (c.getCount() > 0) {
c.moveToFirst();
//while (!c.()){
for (int i = 0; i < 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 your 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 your 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 really bugging me..need help to solve this problem..not quite familiar with layout control.