以编程方式将列表中的项目添加到TableLayout [英] Programmatically adding items from a list to a TableLayout

查看:82
本文介绍了以编程方式将列表中的项目添加到TableLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在进行的Android项目.我是Android开发的新手,因此MainActivity.java文件中有一些代码,我正在为其创建表布局,但是目前我的代码方式是只能从该列表中添加一项,我可以在该列表中做些什么. Activity.java类可以对此进行更改,因此它将以编程方式将我所有的Mcdonalds对象也添加到Table中.如何将两行都添加到MainActivity.java的视图中?

I have an Android project I am working on. I am fairly new to Android Development so I have some code in a MainActivity.java file that I am working on Creating a Table Layout for but the way my code currently is I can only add one item from that list what can I do in the Activity.java class to change this so it will programmatically add all of my Mcdonalds objects too the Table. What can I do to add both rows to the view on MainActivity.java?

Activity_Main.xml

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

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));添加两个项目的方法,但这会引发错误.

As seen in the code above there should be two items in the list and I need a way to add both items with the ll.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); but this will throw a error.

推荐答案

您非常接近,但您需要做的是:

You are pretty close but What you have to do is:

  1. 从xml中删除TableRow.
  2. 总是在循环中创建一个新的TableRow.
  3. 您所有的TextView都需要由setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  4. 设置的布局参数
  5. 将表行也循环添加到表布局中
  6. 我想说MATCH_PARENT对于每个表行宽度都更好
  1. Remove your TableRow from your xml.
  2. Always create a new TableRow in a loop.
  3. All your TextViews need a layout params set by setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  4. Add table row to a table layout also in a loop
  5. I would say MATCH_PARENT would be better for each table row width

另一种方法是使用xml文件,并在其中包含textViews.使用LayoutInflater.from(context).inflate(...)对xml进行充气,填充所有3个通过id查找它们的textview,然后将此膨胀的tableRow添加到tableLayout

The other way to do it would be to make an xml file with and it's textViews inside of it. Inflate the xml with LayoutInflater.from(context).inflate(...), fill all 3 textviews finding them by id and add this inflated tableRow to a tableLayout

这篇关于以编程方式将列表中的项目添加到TableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆