我有一个包含两列的listview,我想为它们添加“Name”和“Age”标题,请帮助我。谢谢。
可以使用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 :(得分:0)
我认为这只是因为您没有将View字段应用为适当的值。 你看我从来没有为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>