2013-07-29 21 views
2

我正在嘗試使用基於數組圖標的動態行數填充TableLayout,但是我無法獲得要在屏幕上顯示的行。該表應顯示一個5x3的圖標網格。我使用的代碼如下:如何在Android中使用動態行數創建表

Services serv = new Services(); 

    Item[] iconArray = serv.getIcons(); 

    TableLayout table = (TableLayout) findViewById(R.id.table1); 

    int rowNum = iconArray.length/5; 
    TableRow rows[]= new TableRow[rowNum]; 

    int count = 0; 
    for (int i = 0; i < iconArray.length; i++) { 
     final String name = iconArray[i].name; 
     final int icon = iconArray[i].icon; 

     LayoutInflater inflater = LayoutInflater.from(MainActivity.this); 
     View v = inflater.inflate(R.layout.icon, null,false); 
     TextView text = (TextView) v.findViewById(R.id.text); 
     text.setText(name); 

     ImageView imageicon = (ImageView) v.findViewById(R.id.icon); 
     imageicon.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       //Do some stuff 
      } 
     }); 
     Drawable drawicon = getResources().getDrawable(icon); 
     imageicon.setBackgroundDrawable(drawicon); 

     if(i == 4) //End of Row 
     { 
      count++; 
     } 
     if(i == 9) //End of Row2 
     { 
      count++; 
     } 

     rows[count] = new TableRow(this); 
     rows[count].addView(v); 
     table.addView(rows[count]); 
    } 

這不輸出任何東西到屏幕和TableLayout保持爲空。任何人都可以告訴我,如果我在正確的路線?

任何幫助,將不勝感激。

感謝

+0

你能解釋一下你的TableLayout必須顯示?只有使用Image和TextView的行? – Jackyto

+0

@Jahckyto TableLayout應該顯示三行填充圖標和一個帶有每個圖標名稱的TextView。使用GridView不是一個選項,因爲其他原因 – user2060733

回答

1

首先,我會做出具體佈局爲我行:

my_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myRow" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:weightSum="1" > 

    <ImageView 
     android:id="@+id/myImg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/myText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"" /> 

</TableRow> 

然後,我將只需填寫我的表,我行在一個循環中:

static TableRow row; 
Item[] iconArray = serv.getIcons(); 
TableLayout table = (TableLayout) myView.findViewById(R.id.table1); 

for (int i = 0; i < iconArray.length; i++) { 

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this); 
    row = (TableRow) inflater.inflate(R.layout.my_row, cont, false); 

    final String name = iconArray[i].name; 
    final int icon = iconArray[i].icon; 

    TextView text = (TextView) v.findViewById(R.id.myText); 
    text.setText(name); 

    ImageView imageicon = (ImageView) v.findViewById(R.id.myImg); 
    imageicon.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      //Do some stuff 
     } 
    }); 
    Drawable drawicon = getResources().getDrawable(icon); 
    imageicon.setBackgroundDrawable(drawicon); 

    table.addView(row); 
} 
+0

感謝您的回答,但我相信這是爲每個圖標創建一個新行。我需要的是5x3網格中的3行。這是我的錯,我沒有在我的原始問題中明確說明。 – user2060733

+0

是的,它爲每個圖像創建一行。我不知道你是否可以創建一個GridLayout表,在這種情況下,你將很容易繪製你的表格 – Jackyto

相關問題