我有一個表格佈局,根據JSON給出的數據構建表格行。我想在按下後突出顯示一行,但我需要在每個給定時間只顯示一行,這意味着如果我突出顯示一行然後按另一行,最後一行應該突出顯示,而第一行不應該突出顯示。一切工作正常,但我不能得到一行,突出後不會突出顯示。這是我的代碼:根據點擊更改表格行的背景顏色
TableLayout tv = (TableLayout) findViewById(R.id.tblFrnd);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < ans.length(); i++) {
TableRow tr = new TableRow(AddFriend.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b3 = new TextView(AddFriend.this);
b3.setText("Display Name");
b3.setTextColor(Color.BLUE);
b3.setTextSize(20);
b3.setPadding(10, 0, 0, 0);
tr.addView(b3);
TextView b4 = new TextView(AddFriend.this);
b4.setPadding(50, 0, 0, 0);
b4.setTextSize(20);
b4.setText("User Name");
b4.setTextColor(Color.BLUE);
tr.addView(b4);
TextView b5 = new TextView(AddFriend.this);
b5.setPadding(90, 0, 0, 0);
b5.setTextSize(20);
b5.setText("ID");
b5.setVisibility(View.INVISIBLE);
b5.setTextColor(Color.BLUE);
tr.addView(b5);
tv.addView(tr);
final View vline = new View(AddFriend.this);
vline.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = ans.getJSONObject(i);
TextView b = new TextView(AddFriend.this);
String str = json_data.getString("DisplayName");
b.setText(str);
b.setPadding(10, 0, 0, 0);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(AddFriend.this);
b1.setPadding(50, 0, 0, 0);
b1.setTextSize(15);
String str1 = json_data.getString("UserName");
b1.setText(str1);
b1.setTextColor(Color.RED);
tr.addView(b1);
final TextView b2 = new TextView(AddFriend.this);
b2.setPadding(90, 0, 0, 0);
b2.setTextSize(15);
String str2 = String.valueOf(json_data.getInt("UserID"));
b2.setText(str2);
b2.setVisibility(View.INVISIBLE);
b2.setTextColor(Color.RED);
tr.addView(b2);
tr.setClickable(true);
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(getResources().getColor(R.color.Yellow));
}
});
tv.addView(tr);
final View vline1 = new View(AddFriend.this);
vline1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
提前致謝!
代碼和問題似乎沒有關聯...... – TheJavaCoder16