0

我在 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);

            }               
4

3 回答 3

3

您正在循环中创建多个textViewfor但仅将文本设置为第一个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变量,您正在多次设置文本。

于 2013-10-16T11:34:21.810 回答
0

我希望这对你有帮助

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

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

私人无效 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>

于 2018-10-26T05:31:56.907 回答
-1

尝试这个

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); 
}
于 2013-10-16T11:32:14.740 回答