0

I have one problem to create my application in Android. Here in my application I bind the ArrayList Data into TableLayout. When I run the application, only the last column of data is bound. I have required all columns to be bound. Please help me...

I have provided my code:

View Task.xml:

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

View 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 Answers 3

3

You're creating multiple textView in the for loop but setting text only to the first 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);

Observe that with the taskdate variable, you are setting text multiple times.

Sign up to request clarification or add additional context in comments.

Comments

0

i hope this will be helpful for you

=======================Main Activity=========================================

    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);
    }
}

Model class like

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;
}

}

Layout file like:

<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>

Comments

-1

Try This

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); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.