我想在SimpleCursorAdapter中显示SQLite数据,结果老是报错,请高手指教,代码我下面贴出来SqlDemo.javapackage org.lhz.dictionary;import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;public class SqlDemo extends SQLiteOpenHelper{
public static final int VERSION  = 1;
private final static String DATABASE_NAME = "Database_test.db";
public SqlDemo(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
public SqlDemo(Context context){
this(context, DATABASE_NAME, VERSION);
}

public SqlDemo(Context context, String name,int version){
this(context, name,null,version);
} @Override
public void onCreate(SQLiteDatabase db) {
System.out.println("Creat a Database.");
db.execSQL("create table history(english varchar(20),chinese varchar(20))");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor query(String sql, String[] args)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, args);
return cursor;
}
==================================================================================================
NewWorld.java
package org.lhz.dictionary;import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;public class NewWord extends ListActivity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
SqlDemo dbService = new SqlDemo(this);
SQLiteDatabase db = dbService.getReadableDatabase();
// Cursor cursor = dbService.query("select english as _id from history",null);
Cursor cursor = db.query("history", new String[]{"english","chinese"}, null, null, null, null, null);
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
new String[]
{"english","chinese" }, new int[]
{ android.R.id.text1,android.R.id.text1});

setListAdapter(simpleCursorAdapter);
}
}
数据库内容的插入是在其他地方,SimpleCursorAdapter只是想单纯的读取并显示,但是各种报错......求高手指点,非常感谢!

解决方案 »

  1.   

    错误信息是
    FATAL EXCEPTION:main
    java.lang.RuntimeException:Unable to start activity CompentInfo{org.lhz.dictionary/NewWord};java.lang.IllegalAgrumentException:column '_id' does not exits
      

  2.   

    我把查询语句改成了
    Cursor cursor = dbService.query("select english as _id from history",null);
    SimpleCursorAdapter改成了
    SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_expandable_list_item_1, cursor,
    new String[]
    {"english","_id"}, new int[]
    { android.R.id.text1,android.R.id.text1});
    现在报错:column 'english' does not exits,但是数据库确实创建好了而且这一列是存在的...
      

  3.   

    我把查询语句改成了
    Cursor cursor = dbService.query("select * from history",null);
    int len = cursor.getCount();
    if(cursor == null || len <= 0){
      Log.e("***","无记录");
    }else{
       SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
       android.R.layout.simple_expandable_list_item_1, cursor,
       new String[]
       {"english","chinese"}, new int[]
       { android.R.id.text1,android.R.id.text1});}试试这样行不!
      

  4.   

    谢谢帮忙!
    我改了下还是报错
    column '_id' does not exits
      

  5.   

    我在创建表的语句里加了一列[_id] AUTOINC,然后用你的查询语句,不报错了,但就是屏幕上什么都不显示