2 つの列を持つリストビューがあり、それらに「名前」と「年齢」ヘッダーを追加したいのですが、親切に助けてください。ありがとうございました。
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>
リストビュー.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);
}
}