如何从tablelayout更改特定TableRow的背景颜色?

时间:2014-07-28 12:26:20

标签: android

我想从表格布局中更改特定表格行的背景颜色。 我的tablerow每行包含三个textview。但它抛出异常java.lang.ClassCastException:android.view.View无法强制转换为android.widget.TableRow

  barcodeid.addTextChangedListener(new TextWatcher()
    {
        public void onTextChanged(CharSequence s, int start, int before,int count)
        {               
            if(!s.equals("") )
            {   
              String s1 = s.toString();
                    int s2 = Integer.parseInt(s1);                  

                    for(int i=0;i<barcode.length;i++)
                    {                       
                        if(s2 == barcode[i])
                        {                                               
                            TableRow row = (TableRow) tv.getChildAt(barcode[i]);                                                        
                            row.setBackgroundColor(Color.GREEN);                                                                                                    
                        }
                    }
            }
        }
    });

//这就是我在tablerow中添加行的方式

   TableRow data_row=new TableRow(DbselectActivity.this);
  data_row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,     TableRow.LayoutParams.WRAP_CONTENT));

   TextView b4=new TextView(DbselectActivity.this); 
   b4.setPadding(10, 0, 0, 0);                                  
   b4.setText(String.valueOf(json_data.getInt("BarcodeId")));
   barcode[i]= Integer.parseInt(String.valueOf(json_data.getInt("BarcodeId")));
   b4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
   b4.setTextColor(Color.WHITE);
   b4.setTextSize(20);
   data_row.addView(b4);

                                TextView b5=new TextView(DbselectActivity.this);
                                b5.setPadding(10, 0, 0, 0);
                                b5.setTextSize(20);
                                b5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));                                   
                                b5.setText(String.valueOf(json_data.getString("ProductName")));
                                b5.setTextColor(Color.WHITE);
                                data_row.addView(b5);

                                TextView b6=new TextView(DbselectActivity.this);
                                b6.setPadding(10, 0, 0, 0);                                 
                                b6.setText(String.valueOf(json_data.getString("ProductType")));
                                b6.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                                b6.setTextColor(Color.WHITE);
                                b6.setTextSize(20);
                                data_row.addView(b6);

                                tv.addView(data_row);

提前致谢

0 个答案:

没有答案