2012-11-20 158 views
3

我有一些麻煩,以編程方式將列添加到表中,也許我查找/搜索錯誤的關鍵字,但找不到合適的解決方案。以編程方式向列添加列

我得到這個代碼:

ScrollView sv = new ScrollView(this); 

     TableLayout ll=new TableLayout(this); 
     HorizontalScrollView hsv = new HorizontalScrollView(this); 
     TableRow tbrow=new TableRow(this); 
     for(int i=0;i<mConnector.idArray.size();i++) { 
      tbrow=new TableRow(this);   

       TextView tv1=new TextView(this); 
       //String s1 = Integer.toString(i); 
       try { 
       String insecticide = mConnector.insecticideArray.get(i); 
       String wegedoornuis = mConnector.wegedoornluisArray.get(i); 
       String dosering = mConnector.doseringArray.get(i); 
       String prijs = mConnector.prijsArray.get(i); 
       String bestuivers = mConnector.bestuiversArray.get(i); 
       String roofvijanden = mConnector.roofvijandenArray.get(i);      

       // String result = insecticide +" | "+wegedoornuis+" | "+dosering+" | "+prijs+" | "+bestuivers+" | "+roofvijanden; 
       // int id = Integer.parseInt(s3); 
       tv1.setId(i); 

       tv1.setText(id); 
       tbrow.addView(tv1); 
       }catch(java.lang.IndexOutOfBoundsException e){ 

       } 

      ll.addView(tbrow); 
     } 
     hsv.addView(ll); 
     sv.addView(hsv); 
     setContentView(sv); 

我想要的是,每一個字符串(try{後)得到他自己的專欄。 所以像String result(現在在評論標誌)。但後來我可以調整列的寬度。

希望你們知道我的意思,否則我總是可以添加圖像來說明。 編輯 做出了圖像快:

enter image description here

回答

5

你不能直接添加到字符串的觀點,但你可以使用文本視圖做同樣的事情。我會告訴你一個我曾經做過的例子,並讓你弄清楚如何爲你自己使用它。

 TableRow row=new TableRow(this.getApplicationContext()); 
     TableLayout tlayout=new TableLayout(this.getApplicationContext()); 

     tlayout.setGravity(Gravity.CENTER); 
     tlayout.setBackgroundResource(R.color.background); 

     row=new TableRow(this); 

     TextView text1=new TextView(this.getApplicationContext()); 
     TextView text2=new TextView(this.getApplicationContext()); 
     TextView text3=new TextView(this.getApplicationContext()); 

     text1.setText(R.string.name); 
     row.addView(text1); 
     text2.setText(R.string.level); 
     row.addView(text2); 
     text3.setText(R.string.score); 
     row.addView(text3); 
     tlayout.addView(row); 
     for (i=0;i<10;i++) 
     { 
      row=new TableRow(this.getApplicationContext()); 
      text1=new TextView(this.getApplicationContext()); 
      text2=new TextView(this.getApplicationContext()); 
      text3=new TextView(this.getApplicationContext()); 


      text1.setText(high_score_name.get(i)); 
      row.addView(text1); 
      text2.setText(""+high_score_level.get(i)); 
      row.addView(text2); 
      text3.setText(""+high_score_score.get(i)); 
      row.addView(text3); 
      tlayout.addView(row); 
     } 

本質上,high_scores_....get(i)返回一個字符串。非常相似的東西應該適合你的目的。

輸出將如下所示,繼續向下10行。

name1 score1 level1 
name2 score2 level2 
+0

如果我看到這個,那麼'row.addView'是一列嗎? – Bigflow

+0

基本上,這將顯示3列數據,每個'TextView'。 – PearsonArtPhoto

+0

好的,但最後一件事。 「谷歌」說專欄也是一排是不是有點愚蠢? (這是我讀的)。我的意思是一列和一行是兩個不同的東西。 – Bigflow

相關問題