代码之家  ›  专栏  ›  技术社区  ›  yams

以编程方式将列表中的项目添加到TableLayout

  •  1
  • yams  · 技术社区  · 11 年前

    我正在做一个Android项目。我对Android开发还很陌生,所以我在MainActivity.java文件中有一些代码,我正在为其创建表格布局,但目前我的代码只能从该列表中添加一项。我可以在Activity.java类中做些什么来更改这一点,这样它就会以编程方式将我所有的Mcdonalds对象也添加到表格中。如何将这两行添加到MainActivity.java上的视图中?

    活动_主.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>
    

    主要活动.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 回复  |  直到 7 年前
        1
  •  1
  •   Mark    11 年前

    你很接近,但你要做的是:

    1. 从xml中删除TableRow。
    2. 始终在循环中创建一个新的TableRow。
    3. 所有的TextViews都需要一个布局参数,由 setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) ;
    4. 在循环中将表行添加到表布局中
    5. 我认为MATCH_PARENT对于每个表的行宽度都会更好

    另一种方法是用创建一个xml文件,其中包含textViews。用LayoutInflater.from(context).full(…)对xml进行充气,填充所有3个按id找到的文本视图,并将这个充气的tableRow添加到tableLayout中