想写一个通过listview显示数据库表中内容的程序,可是老是错误,自己实在想不出来,恳请帮助
以下为源码:
=================================================
数据库:
public class MySQLiteHelper extends SQLiteOpenHelper{
       
        MySQLiteHelper(Context context, String name, CursorFactory factory,
        int version) {
        super(context, name, factory, version);
        }        @Override
        public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists clxx("
                    + "name text ,"
                    + "sex text,"
                    + "type text,"
                    + "_id integer primary key autoincrement,"
                    + "carname text,"
                    + "color text,"
                    + "carid text,"
                    + "created text )");
        db.execSQL("create table if not exists cost("
                + "costid varchar primary key,"
                + "yh float,"
                + "carname varchar,"
                + "rmb float,"
                + "time varchar)");     db.execSQL("create table if not exists byjl("
                + "byid varchar primary key,"
                + "bylist varchar,"
                + "bycost varchar,"
                + "carname varchar,"
                + "other varchar,"
                + "time varchar)");
     
     
}
       
                @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("drop table if exists clxx");
                db.execSQL("drop table if exists cost");
                db.execSQL("drop table if exists byjl");
                onCreate(db);
        }
    }=====================================================
在下面这个clxx.class里实现功能
public class clxx extends Activity {
    /** Called when the activity is first created. */
        public static final String DB_NAME = "car.db";
        public static final int VERSION = 1;  
        public static final String col[]={"name","sex","type","_id","carname","color","carid","created"};
        public static final int ITEM0 = Menu.FIRST;
        public static final int ITEM1 = Menu.FIRST + 1;
        public static final int ITEM2 = Menu.FIRST + 2;
    //MySQLiteHelper cardb;    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clxx_list);
        setTitle("车辆信息");
        MySQLiteHelper helper = new MySQLiteHelper(this,DB_NAME, null,VERSION);
        SQLiteDatabase db=helper.getReadableDatabase();
        ListView list = (ListView) findViewById(R.id.clxxList);
        Cursor clxxCursor=db.query("clxx", col, null, null, null, null,null);
            startManagingCursor(clxxCursor);
            String[] from=new String[]{"carname","created"};
            int[] to=new int[] {R.id.carname,R.id.created};
            SimpleCursorAdapter notes=new SimpleCursorAdapter(this,R.layout.clxx_list,clxxCursor,from,to);
            list.setAdapter(notes);
        
        }
=====================================================
clxx_list.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"
        android:id="@+id/clxx_list">
<ListView android:layout_width="wrap_content"   
   android:layout_height="wrap_content"   
   android:id="@+id/clxxList"  />  <TextView   
   android:text="点击Menu添加车辆信息"   
   android:layout_height="wrap_content"   
   android:textSize="20dip"   
   android:layout_width="fill_parent"   
   android:id="@+id/carname"  
/>  
<TextView   
   android:text=""   
   android:layout_height="wrap_content"   
   android:layout_width="fill_parent"   
   android:layout_below="@+id/ItemTitle"   
   android:id="@+id/created"  
   />  
</LinearLayout>
====================================================
Cursor clxxCursor=db.query("clxx", col, null, null, null, null,null);
            startManagingCursor(clxxCursor);
            String[] from=new String[]{"carname","created"};
            int[] to=new int[] {R.id.carname,R.id.created};
            SimpleCursorAdapter notes=new SimpleCursorAdapter(this,R.layout.clxx_list,clxxCursor,from,to);
            list.setAdapter(notes);
我注释掉“SimpleCursorAdapter notes=new SimpleCursorAdapter(this,R.layout.clxx_list,clxxCursor,from,to);”这句话就可以运行,把clxxCursor换成null也能够运行,我不明白是我的cursor写的有问题吗?自己实在是解决不了了,求大大们帮帮小弟吧

解决方案 »

  1.   

    先确认你的cursor内容是OK的,如果OK,那就是你布局文件的问题了,一般我们都是自己写个继承baseadapter的adapter,然后在getview里去指定每个listitem显示的内容。
      

  2.   

    我就是想通过cursor获得数据库表中的内容
    自己觉得写的没错:
    public static final String col[]={"name","sex","type","_id","carname","color","carid","created"};
    Cursor clxxCursor=db.query("clxx", col, null, null, null, null,null);
    数据库那个表如下:
    db.execSQL("create table if not exists clxx("
      + "name text ,"
      + "sex text,"
      + "type text,"
      + "_id integer primary key autoincrement,"
      + "carname text,"
      + "color text,"
      + "carid text,"
      + "created text )");
      

  3.   

    貌似用SimpleCursorAdapter直接关联到listview上效率高,去百度面试时被问道为什么不直接用CursorAdapter,结果就挂了.