Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add MultiColumn Header for ListView
    text
    copied!<p>I have a listview with two columns, and I want to add "Name" and "Age" headers for them, please kindly help me. Thank you. </p> <p>Is that possible to use addHeaderView()? </p> <p>main.xml</p> <pre><code>&lt;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" &gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>listview.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;TextView android:text="Name" android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/name" android:textSize="30sp" &gt; &lt;/TextView&gt; &lt;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"&gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>DbHelper.java</p> <pre><code>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); } } </code></pre> <p>MainActivity.java</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload