2014-07-24 36 views
-1

我有一个表格布局,根据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); 
    } 

提前致谢!

+0

代码和问题似乎没有关联...... – TheJavaCoder16

回答

1
  • int selectedRow = -1在课堂上。
  • 类中的添加功能:

    private void updateHighlightedRow() 
    { 
        // loop through the rows and set the background 
        // for a row to highlighted color if the row index == selectedRow 
        // else sent normal bg color 
    } 
    
  • 同时添加一行,使该行可点击和点击监听器设置行索引selectedRow,然后调用updateHighlightedRow

这应该做这些事。

+0

如何循环遍历行以设置其背景?在for函数中写什么? –

+1

使用'TableLayout'对象的'getChildCount'和'getChildAt'方法循环 – spgodara

0

使textview setClickable(false)。

相关问题