如何在Android中将数据从ArrayList绑定到TableLayout?

时间:2013-10-16 11:25:04

标签: android arraylist

我在Android中创建应用程序时遇到一个问题。 在我的应用程序中,我将ArrayList数据绑定到TableLayout中。 当我运行应用程序时,只绑定了最后一列数据。我已经要求绑定所有列。请帮帮我......

我提供了我的代码:

查看Task.xml:

<TableLayout
                android:id="@+id/score_table1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TableRow></TableRow>
 </TableLayout>

查看task.java:

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

            for(int i=0;i<object.size();i++)
            {


                    TableRow row=new TableRow(ViewTask.this);

                    @SuppressWarnings("unchecked")
                    ArrayList<String> data1 = (ArrayList<String>) object.get(i);

                    TextView taskdate = new  TextView(ViewTask.this);
                    taskdate.setTextSize(10);
                    taskdate.setText(data1.get(0).toString());
                    row.addView(taskdate);

                    TextView title = new  TextView(ViewTask.this);
                    taskdate.setText(data1.get(1).toString());
                    row.addView(title);
                    taskdate.setTextSize(10);


                    TextView taskhour = new  TextView(ViewTask.this);
                    taskdate.setText(data1.get(2).toString());
                    taskhour.setTextSize(10);
                    row.addView(taskhour);


                    TextView description = new  TextView(ViewTask.this);
                    taskdate.setText(data1.get(3).toString());
                    row.addView(description);
                    description.setTextSize(10);

                    table.addView(row);

            }               

3 个答案:

答案 0 :(得分:3)

您在textView循环中创建了多个for,但只将文字设置为第一个textTiew

TextView taskdate = new  TextView(ViewTask.this);
taskdate.setTextSize(10);
taskdate.setText(data1.get(0).toString());
row.addView(taskdate);

TextView title = new  TextView(ViewTask.this);
taskdate.setText(data1.get(1).toString());
row.addView(title);
taskdate.setTextSize(10);

TextView taskhour = new  TextView(ViewTask.this);
taskdate.setText(data1.get(2).toString());
taskhour.setTextSize(10);
row.addView(taskhour);

TextView description = new  TextView(ViewTask.this);
taskdate.setText(data1.get(3).toString());
row.addView(description);
description.setTextSize(10);

使用taskdate变量观察,您要多次设置文本。

答案 1 :(得分:0)

试试这个

ArrayList<String> data1 = new ArrayList<String>();
 TableLayout table ;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

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

    for (int i = 0; i < object.size(); i++) {
    TableRow row= new TableRow(this);

    TextView taskdate = new TextView(this);
    row.addView(ch);
    createView(row, taskdate , tablesName.get(i));
    tl.addView(tr);

TextView title = new TextView(this);
    row.addView(ch);
    createView(row, title , tablesName.get(i));
    tl.addView(tr);
}   

    public void createView(TableRow tr, TextView t, String viewdata) {
    t.setText(viewdata);
    t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    t.setTextColor(Color.DKGRAY);
    t.setPadding(20, 0, 0, 0);
    tr.setPadding(0, 1, 0, 1);
    tr.addView(t); 
}

答案 2 :(得分:0)

我希望这对您有帮助

=======================主要活动======================= ==================

    List<Product>productList;
    Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setValues();
    init();
}

private void setValues(){

    productList=new ArrayList<>();
    productList.add(new Product("PR-1", "C-1", "A2 Milk 1 ltr", 99));
    productList.add(new Product("PR-2", "C-1", "A2 Ghee 500 gm", 1500));
    productList.add(new Product("PR-3", "C-2", "Full Cream Milk 1 ltr", 90));
    productList.add(new Product("PR-4", "C-2", "Raw Natural Milk 1 ltr", 85));
    productList.add(new Product("PR-5", "C-2", "Skimmed Milk 1 ltr", 80));
}

private void init() {
    TableLayout stk = (TableLayout) findViewById(R.id.table_main);
    TableRow tbrow0 = new TableRow(this);
    TextView tv0 = new TextView(this);
    tv0.setText(" Pr.No ");
    tv0.setTextColor(Color.WHITE);
    tbrow0.addView(tv0);
    TextView tv1 = new TextView(this);
    tv1.setText("Category ");
    tv1.setTextColor(Color.WHITE);
    tbrow0.addView(tv1);
    TextView tv2 = new TextView(this);
    tv2.setText("\t\t Name");
    tv2.setTextColor(Color.WHITE);
    tbrow0.addView(tv2);
    TextView tv3 = new TextView(this);
    tv3.setText("Price ");
    tv3.setTextColor(Color.WHITE);
    tbrow0.addView(tv3);
    stk.addView(tbrow0);
    for (int i = 0; i < productList.size(); i++) {
        TableRow tbrow = new TableRow(this);
        TextView t1v = new TextView(this);
        t1v.setText("" + productList.get(i).getCode());
        t1v.setTextColor(Color.WHITE);
        t1v.setGravity(Gravity.CENTER);
        tbrow.addView(t1v);
        TextView t2v = new TextView(this);
        t2v.setText(productList.get(i).getCategoryCode());
        t2v.setTextColor(Color.WHITE);
        t2v.setGravity(Gravity.CENTER);
        tbrow.addView(t2v);
        TextView t3v = new TextView(this);
        t3v.setText(productList.get(i).getName());
        t3v.setTextColor(Color.WHITE);
        t3v.setGravity(Gravity.CENTER);
        tbrow.addView(t3v);
        TextView t4v = new TextView(this);
        t4v.setText(""+productList.get(i).getPrice());
        t4v.setTextColor(Color.WHITE);
        t4v.setGravity(Gravity.CENTER);
        tbrow.addView(t4v);
        stk.addView(tbrow);
    }
}

像这样的模型类

public class Product {
private String code;
private String categoryCode;
private String name;
private double price;

public Product(String code, String categoryCode, String name, double price) {
    this.code = code;
    this.categoryCode = categoryCode;
    this.name = name;
    this.price = price;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getCategoryCode() {
    return categoryCode;
}

public void setCategoryCode(String categoryCode) {
    this.categoryCode = categoryCode;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

}

布局文件,如:

<ScrollView 
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_alignParentLeft="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<HorizontalScrollView
    android:id="@+id/horizontal_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/table_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >
        </TableLayout>
    </RelativeLayout>
</HorizontalScrollView>