2011-08-01 81 views
0

我有一个小问题。我有一个工具,将likly有应显示在布局渠道的大的变动数。所以我必须创建TextView来显示代码中的值。其中像魅力工作。访问代码生成的TextView资源

我的问题:

  1. 如何访问所创建的 “valueTV” 场在我的代码?具体来说,我想要写加速度计进去,其被存储在字符串SAccX的值。

  2. 有没有看到创建的领域与R.id.XXX在Eclipse的DDMS或调试窗口的方法?

如果我尝试用分配访问它像一个正常的XML对象的Eclipse给我一个错误(类似“sAccX = (TextView) findViewById(R.id.valueTV201);)。我明白为什么这个错误就要到了,但我不知道怎么弄围绕它;-)

//Get Tablelayout 
    TableLayout ChannelTable = (TableLayout) findViewById(R.id.TableChannels); 

    //Create a new Row for every Channel 
    for (int current = 0; current < numberOfChannels; current++) 
    { 
     // Create a Table with new ID 
     TableRow tr = new TableRow(this); 
     tr.setId(100 + current); 
     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     // Create a TextView to show the Name of the Channel 
     TextView TVChannels = new TextView(this); 
     TVChannels.setId(200 + current); 
     TVChannels.setText(channels[current]); 
     TVChannels.setTextColor(Color.DKGRAY); 
     TVChannels.setTextSize(20); 
     TVChannels.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tr.addView(TVChannels); 
      // Create a TextView to house the values 
     TextView valueTV = new TextView(this); 
     valueTV.setId(200 + current);  
     valueTV.setTextColor(Color.DKGRAY); 
     TVChannels.setTextSize(20); 
     valueTV.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tr.addView(valueTV); 

     // Add the TableRow to the TableLayout 
     ChannelTable.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 
} /* End of OnCreate*/ 

回答

0

使用标签,而不是ID的

View.setTag() 

,然后找到与某个标签使用视图

findViewWithTag() 
+0

我很抱歉,但我不明白的差异的ID的。除了不需要在layout.xml中声明的事实之外,标签是否等同于ID? – Rotesmofa