2013-03-18 47 views
0

我有一個我正在開發的Android項目。我對Android開發相當陌生,所以我在MainActivity.java文件中有一些代碼,我正在創建Table Layout,但目前我的代碼的方式是隻能從該列表添加一個項目,我可以在Activity.java類來改變這個,所以它會以編程的方式添加我所有的麥當勞對象。我可以做什麼來將兩行添加到MainActivity.java上的視圖?以編程方式將列表中的項目添加到TableLayout中

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".ShelterActivity" > 

<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0"> 

    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Address" 
     android:width="250px" 
     android:textStyle="bold"></TextView> 
    <TextView 
     android:id="@+id/TextView02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Distance" 
     android:width="250px" 
     android:textStyle="bold"></TextView> 
    <TextView 
     android:id="@+id/TextView03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ETA" 
     android:width="250px" 
     android:textStyle="bold"></TextView> 
    </TableRow> 
</TableLayout> 
</LinearLayout> 

MainActivity.java

  protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_shelter); 
     List<McDonalds> mcdList = new ArrayList<McDonalds>(); 

       ScrollView sv = new ScrollView(this); 

        // get a reference for the TableLayout 
        TableLayout ll = (TableLayout) findViewById(R.id.TableLayout01); 

        HorizontalScrollView hsv = new HorizontalScrollView(this); 
       // create a new TableRow 
        TableRow row = new TableRow(this); 


     //This will be replaced by a Read Only Stored procedure that gets the List of Shelters 
     McDonalds mcdonalds = new McDonalds("720 N Main St, Roswell, NM", 1.08, 8.0); 
     mcdList.add(mcdonalds); 
     McDonalds mcdonalds1 = new McDonalds("1804 S Main St, Roswell, NM", 2.9, 12.0); 
     mcdList.add(mcdonalds1); 
     for (McDonalds mcD : mcdList){ 
       // create a new TextView 
        TextView t = new TextView(this); 
        TextView t1 = new TextView(this); 
        TextView t2 = new TextView(this); 
     String mcdAddress = mcD.getAddress(); 
     String distance = mcD.getDistance().toString(); 
     String ETA = mcD.getETA().toString(); 
     t.setText(mcdAddress); 
     t1.setText(distance); 
     t2.setText(ETA); 
     row.addView(t); 
     row.addView(t1); 
     row.addView(t2); 


    } 
    // add the TableRow to the TableLayout 
    ll.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

} 

如上代碼看到上面應該有列表中的兩個項目,我需要一種方法來與ll.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));添加兩個項目但是這會引發錯誤。

回答

1

你是相當接近,但你必須做的是:

  1. 從XML中刪除您的TableRow。
  2. 總是在循環中創建一個新的TableRow。
  3. 所有的TextView都需要一個由setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))設置的佈局參數;
  4. 添加錶行到表的佈局也處於環
  5. 我會說MATCH_PARENT將每個錶行寬度

的另一種方式做到這一點是使一個XML文件更好,它是其中的textViews。使用LayoutInflater.from(上下文).inflate(...)填充xml,填充所有3個textviews,並通過id找到它們,並將此虛擬表格行添加到表格佈局

+0

xml中的表格行僅用於列標題。 – yams 2013-03-18 19:54:39

+0

如果只是列標題 - 那很好。讓我知道一切是否清楚。 – Mark 2013-03-18 19:57:06

+0

你能給我一個代碼示例嗎? – yams 2013-03-18 19:57:09

相關問題