Skip to content Skip to sidebar Skip to footer

Populating A Table With An Array Of Data

I want to set up a table which has a fixed amount of columns, and an X amount of rows with The first row listing the contents of each column. For example, one column will be 'Name'

Solution 1:

I think inside the table layout create the tablerow as below

<TableLayout---
><TableRow--
><Textviewforname
/><Textview...forage
/></TableRow><TableRow--
><Listviewforname
/><Listview...forage
/></TableRow></TableLayout>

and populate the listview with a simple arrayadapter with your fixed data.

this's simple if you have textviews only in your listview you can use

ArrayAdapter ad=newArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,namearray); list1.setAdapter(ad);

ArrayAdapter ad=newArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,agearray); list2.setAdapter(ad);

Solution 2:

A ListView basically acts as a row within any table of data. You should create a POJO with all the attributes you want to display in a row. You can create create a custom xml layout for the data, which would probably by a horizontal LinearLayout that corresponds to your columns.

POJO

publicclassMyData{
    publicString Name;
    publicint Age;
    // More data here
}

ListView item layout (layout_list_item.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/Name"android:layout_height="wrap_content"android:layout_width="0dip"android:layout_weight="0.7" /><TextViewandroid:id="@+id/Age"android:layout_height="wrap_content"android:layout_width="0dip"android:layout_weight="0.3" /></LinearLayout>

Main Layout

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_height="wrap_content"android:layout_width="0dip"android:layout_weight="0.7"android:text="Name" /><TextViewandroid:layout_height="wrap_content"android:layout_width="0dip"android:layout_weight="0.3"android:text="Age" /></LinearLayout><ListViewandroid:id="+@id/MyDataListView"android:layout_width="fill_parent"android:layout_height="fill_parent"/></LinearLayout>

You then need a custom adapter which sets the fields in the list view with the values from your POJO. There are plenty of tutorials on the internet for this.

Post a Comment for "Populating A Table With An Array Of Data"