0

I have class Like

public class Items {
    public int icon;
    public String label;
    public String price;
    public String offer;
    public Items(){
        super();
    }

    public Items(int icon, String label,String price,String offer) {
        super();
        this.icon = icon;
        this.label = label;
        this.price = price;
        this.offer = offer;
    }
}

And I created objects for that class like

Items items_data[] = new Items[]
{
new Items(R.drawable.ic_launcher, "Samsung","400","Hot Item"),
new Items(R.drawable.ic_launcher, "Samsung1","4001","Hot Item1"),

};

Now i need to display the above value in table row like In first row i have to display the first position and so on.. please help.

3
  • 2
    Use ListView to present your data in table-like view, the link contains everything you need to know. Commented Jul 18, 2013 at 7:11
  • Do you want to display this in ListView or in TableLayout with TableRows? Commented Jul 18, 2013 at 7:14
  • yes i want to display it with tablelayout and also the table rows and columns are created based on the length of the array Commented Jul 18, 2013 at 7:38

1 Answer 1

1

You can try with this:

Items items_data[] = new Items[] {
    new Items(R.drawable.ic_launcher, "Samsung", "400", "Hot Item"),
    new Items(R.drawable.ic_launcher, "Samsung1", "4001", "Hot Item1"),
};

TableLayout tl = new TableLayout(this);

for (Items items : items_data) {
    TableRow tr = new TableRow(this);

    ImageView iv = new ImageView(this);
    iv.setBackgroundResource(items.icon);
    tr.addView(iv);

    TextView label = new TextView(this);
    label.setText(items.label);
    tr.addView(label);

    TextView price = new TextView(this);
    price.setText(items.price);
    tr.addView(price);

    TextView offer = new TextView(this);
    offer.setText(items.offer);
    tr.addView(offer);

    tl.addView(tr);
}

your_layout.add(tl);
Sign up to request clarification or add additional context in comments.

2 Comments

It works but it shows in a single column only..If I have 9 records means it has to show in a 3 rows and 3 colums
When you add a view in TableRow it adds every view in the next column. So, this code crates a table with two rows with four columns each. I think what you want to do is show borders between rows and columns. If you want to do this you have to add a border background in the drawable folder. Search, you can find how to do this easily. And after that, apply this drawable as background of every view.

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.