I have populated a listview(4columns) using simpleadapter and hashmap. Now I would like to have each column of the listview a name.
My listview is displayed inside a linear layout with 4 textviews.
Now, in order to have attribute name of each column, I try to have 2 linear layouts, and the top linear layout have 4 textviews with the attribute names, the bottom linear layout with the data from simpleadapter. And put two linear layouts inside a another linear layout.
And this didn’t work the way i want…
I am fairly new to android, please help.
Edit:
Here is my code:
public class MultiList extends ListActivity {
ListView lv;
SimpleAdapter sd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Calendar c = Calendar.getInstance();
lv = getListView();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
map = new HashMap<String, String>();
map.put("Date", ""+c.get(Calendar.DATE));
map.put("Month", ""+c.get(Calendar.MONTH)+1);
map.put("Time", "" + new Date().toString());
aList.add(map);
sd= new SimpleAdapter(this, aList, R.layout.main, new String[]{"Date","Month","Time"}, new int[]{R.id.studentID,R.id.studentAge,R.id.studentName});
lv.setAdapter(sd);
// insertData();
}
My main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student ID" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Age" />
<TextView
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="student Name" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="@+id/studentID"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/studentAge"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/studentName"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
The problem is, each of the attribute name (student ID, student Age, Student Name) is repeated on every row. How do I fix that?
Break your layout into two, the first one will have the column headers. Let’s call it header.xml:
The second one is simply for each row, called row.xml
(You can give them better names later.)
Now your onCreate() should have this: