2011-12-18 27 views
2

我有一點麻煩搞清楚如何從sqlite的數據綁定到列表視圖。
這裏我的整個代碼,我會是一些幫助或建議非常感謝。
這裏的問題是,我發現了以下異常:
拋出:IllegalArgumentException:列「_id」不存在,在SimpleCursorAdapter調用GetCursor時。使用遊標中的數據填充listview?

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">  
    <ListView 
     android:id="@android:id/android:list" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

item_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
<TextView 
    android:id="@+id/name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="24dp" 
    android:padding="6dp" /> 
<TextView 
    android:id="@+id/time" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="18dp" 
    android:padding="1dp" /> 
<TextView 
    android:id="@+id/date" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="18dp" 
    android:padding="4dp" 
    android:gravity="right" /> 
</LinearLayout> 

類實施的SQLite

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteStatement; 
import android.util.Log; 

public class DataHelper { 

private static final String DATABASE_NAME = "example.db"; 
private static final int DATABASE_VERSION = 1; 
private static final String TABLE_NAME = "table1";  

private Context context; 
private SQLiteDatabase db; 
private SQLiteStatement insertStmt; 
private Cursor cursor; 

public DataHelper(Context context) { 
    this.context = context; 
    OpenHelper openHelper = new OpenHelper(this.context); 
    this.db = openHelper.getWritableDatabase(); 
} 

public long insert(String name, String time, String date) { 
    insertStmt = db.compileStatement("insert into table1 values(?,?,?)");  
    this.insertStmt.bindString(1, name); 
    this.insertStmt.bindString(2, time); 
    this.insertStmt.bindString(3, date); 
    return this.insertStmt.executeInsert(); 
} 

public void deleteAll() { 
    this.db.delete(TABLE_NAME, null, null); 
} 

public Cursor GetCursor() { 
    cursor = this.db.query(TABLE_NAME, new String[] { "_id", "name", "time", "date" }, null, null, null, null, "date desc"); 

    return cursor;  
} 

private static class OpenHelper extends SQLiteOpenHelper { 

    OpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, time TEXT, date TEXT);"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w("Example", 
       "Upgrading database, this will drop tables and recreate."); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    } 
    } 
} 

的onCreate類

import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.SimpleCursorAdapter; 

public class SqlTest extends ListActivity { 

private DataHelper dh; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Date now = new Date(); 
    String time = now.getHours() + ":" + now.getMinutes(); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); 
    String date = df.format(now); 

    this.dh = new DataHelper(this); 
    this.dh.deleteAll(); 
    this.dh.insert("Porky Pig", time, date); 
    this.dh.insert("Foghorn Leghorn", time, date); 
    this.dh.insert("Yosemite Sam", time, date); 
    this.dh.insert("SD", time, date); 

    String[] columns = new String[] { "_id", "name", "time", "date" }; 
    int[] to = new int[] {R.id.name, R.id.time, R.id.date}; 

    SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to); 

    this.setListAdapter(mAdap); 
} 
} 
+0

什麼是你有特別關切/問題?什麼不起作用?什麼是?它會崩潰嗎?有日誌消息嗎?只是發佈你的代碼並沒有真正說明這一點。 – dmon

+0

什麼不工作? –

+0

對不起球員,我得到的異常是illegalargument例外稱列「_id」不存在,對於simplecursoradapter。 – Grendizer

回答

1

試試這個:

import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.SimpleCursorAdapter; 

public class SqlTest extends ListActivity { 

private DataHelper dh; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Date now = new Date(); 
    String time = now.getHours() + ":" + now.getMinutes(); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); 
    String date = df.format(now); 

    this.dh = new DataHelper(this); 
    this.dh.deleteAll(); 
    this.dh.insert("Porky Pig", time, date); 
    this.dh.insert("Foghorn Leghorn", time, date); 
    this.dh.insert("Yosemite Sam", time, date); 
    this.dh.insert("SD", time, date); 

    // Don't put _id here 
    String[] columns = new String[] {"name", "time", "date" }; 
    int[] to = new int[] {R.id.name, R.id.time, R.id.date}; 

    SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to); 

    this.setListAdapter(mAdap); 
} 
} 
+0

指定下次plz更改的內容。 – Warpzit

+0

它被指定,閱讀評論 –