如何在列表视图的每一列上创建属性名称?

问题描述:

我已经使用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

将您的布局分为两部分,第一部分将包含列标题。让我们把它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