房间b灯2打开,但房间状态变为绿灯2状态
我使用另一种布局的Layoutinflater创建了我的mainlayout。这将动态添加更多控件。我可以在mainlayout中动态创建任意数量的子布局。问题是如何在当前子布局操作中访问特定的textview。例如,如果我从房间b中的lamp2按下按钮ON,则它旁边的状态将变为绿色或红色。
这是加载我的布局的主要活动
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);
}
}
这真让我烦恼......需要帮助来解决这个问题......对布局控制并不熟悉。
答案 0 :(得分:0)
首先,我建议使用一个ToggleButton
or Switch
而不是2个单独的按钮,但是如果你想要&#34;绑定&#34;对于按钮的TextView状态,您可以简单地创建一个自定义的ClickListener类。
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) { do network operation }
tv.setBackgroundColor(isChecked ? Color.GREEN : Color.RED);
}
};
要使用,请这样做
TextView txtStatus = (TextView) findViewById(R.id.status);
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new MyCheckChangedListener(txtStatus));
尽管如此,关于ToggleButton的好处是你甚至不需要TextView,你可以看到&#34; status&#34;通过查看按钮本身的状态。