我已經使用simpleadapter和hashmap填充了一個listview(4columns)。現在我想讓列表視圖的每一列成爲一個名字。如何在列表視圖的每一列上創建屬性名稱?
我的列表視圖顯示在一個線性佈局中,包含4個文本視圖。
現在,爲了具有每個列的屬性名稱,我嘗試使用2個線性佈局,並且頂部線性佈局有4個帶有屬性名稱的文本視圖,底部線性佈局以及來自simpleadapter的數據。將兩個線性佈局放入另一個線性佈局中。
而這並沒有按照我想要的方式工作... 我是相當新的android,請幫助。
編輯:
這裏是我的代碼:
公共類多重表擴展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();
}
我的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>
的問題是,每個屬性名稱(學生ID,學生年齡,學生姓名)在每一行重複。我如何解決這個問題?
我對你的問題感到困惑,你想在第一行上面添加一個[header](http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View))來顯示每個列名? – Sam 2012-07-22 17:07:58
是的,讓我告訴你我得到了什麼 – 2012-07-22 17:21:48