2012-07-22 32 views
2

我已經使用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,學生年齡,學生姓名)在每一行重複。我如何解決這個問題?

+0

我對你的問題感到困惑,你想在第一行上面添加一個[header](http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View))來顯示每個列名? – Sam 2012-07-22 17:07:58

+0

是的,讓我告訴你我得到了什麼 – 2012-07-22 17:21:48

回答

2

將您的佈局分爲兩部分,第一部分將包含列標題。讓我們把它header.xml

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

二是簡單地爲每一行,叫row.xml

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

(您可以在以後給他們更好的名字。)

現在你的onCreate()應該有這個:

// Set up your header 
lv.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false)); 

// Change the resource id to R.layout.row 
sd = new SimpleAdapter(this, aList, R.layout.row, new String[]{"Date","Month","Time"}, new int[]{R.id.studentID,R.id.studentAge,R.id.studentName}); 
lv.setAdapter(sd); 
+0

聽起來不錯!我正在嘗試 – 2012-07-22 17:29:28

+0

訂單是錯誤的,設置適配器(SD)應與addheaderview交換。 工作就像一個魅力!多謝兄弟! – 2012-07-22 17:38:22

+0

沒有問題。我更新了我的答案,我會記住這一點,謝謝你的更正! – Sam 2012-07-22 17:54:02

相關問題