2011-02-16 70 views
0

我想從字符串的二維數組創建表。不過,我吸引用代碼創建視圖,所以出於某種原因,我的圖像永遠不可見,並且我的文本視圖不會分成多行,而是剔除文本。在android中創建動態表

此外,我在代碼中的每個文本視圖上放置了id,當有人單擊textview時,我希望能夠使用該ID來識別被點擊的whick行。不幸的是,情況並非如此。

有沒有什麼好的方法來識別正確的行?

問候

 for (int current=0; current<cityArr.length; current++) { 

     TableRow tr = new TableRow(this); 
     tr.setId(100+current); 
     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     TextView labelTV = new TextView(this); 
     labelTV.setId(current); 
     labelTV.setText(cityArr[current][0]); 
     labelTV.setTextColor(Color.BLACK); 
     labelTV.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tr.addView(labelTV); 

     ImageView mapView = new ImageView(this); 
     mapView.setImageResource(R.drawable.list_icon_map); 
     mapView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, 5)); 
     tr.addView(mapView); 

     tlcity.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 

回答

1

哈,原來工作好嗎與SETID()函數。只要我把OnClickListener放在正確的組件上...

5
public class MainActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ScrollView sv = new ScrollView(this); 

     TableLayout ll=new TableLayout(this); 
     HorizontalScrollView hsv = new HorizontalScrollView(this); 

     for(int i=1;i<30;i++) { 
      TableRow tbrow=new TableRow(this); 

      for(int j=1;j<=20;j++) { 
       TextView tv1=new TextView(this); 
       String s1 = Integer.toString(i); 
       String s2 = Integer.toString(j); 
       String s3 = s1+s2; 
       int id = Integer.parseInt(s3); 
       tv1.setId(id); 

       tv1.setText("Dynamic TextView no:  "+id); 
       tbrow.addView(tv1); 
      } 
      ll.addView(tbrow); 
     } 
     hsv.addView(ll); 
     sv.addView(hsv); 
     setContentView(sv); 
    } 
}