2013-10-26 62 views
0

我有兩列的列表視圖,我想爲它們添加「名稱」和「年齡」標題,請幫助我。謝謝。如何爲ListView添加多列標題

可以使用addHeaderView()嗎?

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:tools="http://schemas.android.com/tools"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent"   
    android:paddingBottom="@dimen/activity_vertical_margin"   
    android:paddingLeft="@dimen/activity_horizontal_margin"   
    android:paddingRight="@dimen/activity_horizontal_margin"   
    android:paddingTop="@dimen/activity_vertical_margin"   
    tools:context=".MainActivity" >   

    <TextView   
      android:layout_width="wrap_content"   
      android:layout_height="wrap_content"   
      android:text="@string/hello_world" />   

</RelativeLayout>   

listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/Relativelayout01" 
    android:paddingBottom="4dip" 
    android:paddingLeft="12dip"> 

<TextView 
     android:text="Name" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/name" 
     android:textSize="30sp" 
     > 
    </TextView> 

    <TextView 
     android:text="Age" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/age" 
     android:layout_toRightOf="@+id/name" 
     android:layout_below="@+id/name" 
     android:textSize="30sp" 
     android:layout_alignTop="@id/name"> 
    </TextView> 
</RelativeLayout> 

DbHelper.java

package com.myapp3; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DbHelper extends SQLiteOpenHelper{ 
    public static final String DATABASE_NAME ="bebook_db"; 

    public DbHelper(Context context){ 
      super(context,DATABASE_NAME,null,1); 
    } 

    public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE mytable(_id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT,Age INTEGER); "); 

      ContentValues cv = new ContentValues(); 

      cv.put("Name", "Anna"); 
      cv.put("Age", 19); 
      db.insert("mytable", "Name", cv); 

      cv.put("Name", "Jane"); 
      cv.put("Age", 21); 
      db.insert("mytable", "Name", cv); 

      cv.put("Name", "Mary"); 
      cv.put("Age", 17); 
      db.insert("mytable", "Name", cv); 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS mytable"); 
      onCreate(db); 
    } 
} 

MainActivity.java

package com.myapp3; 

import android.os.Bundle; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends ListActivity { 

    private SQLiteDatabase db = null; 
    private Cursor cursor = null; 
    private SimpleCursorAdapter adapter; 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     db= (new DbHelper (getApplicationContext())).getWritableDatabase();   
     cursor =db.rawQuery("SELECT _id,Name, Age from mytable ORDER BY Age", null); 

     adapter = new SimpleCursorAdapter(this, 
         R.layout.listview, 
         cursor, 
         new String[]{"Name","Age"}, 
         new int[]{R.id.name, R.id.age},1); 
     setListAdapter(adapter); 

} 

protected void onDestroy() { 
     super.onDestroy(); 
     cursor.close(); 
     db.close(); 
} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      return true; 
    } 

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
      super.onCreateContextMenu(menu, v, menuInfo); 
    } 
} 

回答

0

我認爲這只是因爲你不應用視圖字段的適當值。 你看我從來沒有開發Android平臺, ,但最上面的一行就像WPF中的XAML。 因此,如果他們類似,我建議你嘗試這樣的事情。

<ListView Name="listView1" Height="246" Width="640" 
VerticalAlignment="Top" HorizontalAlignment="Left" Margin="33,86,0,0"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Name" Width="500" /> 
      <GridViewColumn Header="year" Width="100" /> 
     </GridView> 
    </ListView.View> 
</ListView>