在listview中填充sqlite数据的难度

时间:2014-12-09 20:02:54

标签: android

我是android新手。 我可以在sqlite数据库中存储数据,但我想在listview中检索数据。 但我有空指针异常

listview的Mainctivity.java类。

public class MainActivity extends Activity {

SQLiteDatabase db;

DBAdapter dbh;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abcmain);
    dbh = new DBAdapter(this);
    db = dbh.getWritableDatabase();
    populateListview();  // Here it gives error
}

public void populateListview() {
    SimpleCursorAdapter mycursor;
    db = dbh.getWritableDatabase();
    Cursor cursor;
    cursor = dbh.getAllOil(); // and here it gives error
    startManagingCursor(cursor);
    String[] fromfields = new String[] { DBAdapter.COLUMN_OIL_DATE,
            DBAdapter.COLUMN_OIL_TOTAL_COST, DBAdapter.COLUMN_OIL_LITERS,
            DBAdapter.COLUMN_OIL_CHANGE_LOCATION };
    int[] tofields = new int[] { R.id.oildate, R.id.oilcost,
            R.id.oilliters, R.id.oillocation };
    mycursor = new SimpleCursorAdapter(this, R.layout.abclist_group,
            cursor, fromfields, tofields, 0);
    ListView mylist = (ListView) findViewById(R.id.list_oil);
    mylist.setAdapter(mycursor);

}
}

用于sqlite数据库的DBAdapter.java类。我可以将数据存储在数据库中,并可以在对话框中检索,但我想在listview中找到它

public class DBAdapter extends SQLiteOpenHelper {
public SQLiteDatabase db;
public DBAdapter dbh;
private Context context;

@SuppressLint("SdCardPath")
private static String DB_PATH = "/data/data/com.munawwar.sultan/databases/";
private static final String DATABASE_NAME = "vehicle.db";
private static final int DATABASE_VERSION = 3;

public DBAdapter(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;

    // TODO Auto-generated constructor stub
}

/*
 * ====================================================================
 * TABLE OIL COLUMNS
 * ====================================================================
 */
public static final String TABLE_OIL = "oil";
public static final String COLUMN_OIL_ID = "_oil_id";
public static final String COLUMN_OIL_DATE = "oil_date";
public static final String COLUMN_OIL_TOTAL_COST = "oil_total_cost";
public static final String COLUMN_OIL_LITERS = "oil_liters";
public static final String COLUMN_OIL_CHANGE_LOCATION = "oil_change_location";

/*
 * ====================================================================
 * CREATING OIL TABLE
 * ====================================================================
 */

public static final String DATABASE_CREATE_TABLE_OIL = "create table "
        + TABLE_OIL + "(" + COLUMN_OIL_ID
        + " integer primary key autoincrement, " + COLUMN_OIL_DATE
        + " date not null, " + COLUMN_OIL_TOTAL_COST + " number not null, "
        + COLUMN_OIL_LITERS + " real not null, "
        + COLUMN_OIL_CHANGE_LOCATION + " text not null);";

/*
 * ====================================================================
 * ONCREATE METHOD
 * ====================================================================
 */
@Override
public void onCreate(SQLiteDatabase database) {
    try {

        database.execSQL(DATABASE_CREATE_TABLE_OIL);

    } catch (SQLException e) {

        e.printStackTrace();
    }
    Toast.makeText(context, "oncreate is called", Toast.LENGTH_LONG).show();

}

/*
 * ====================================================================
 * ONUPGRADE METHOD
 * ====================================================================
 */

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_OIL);
    onCreate(db);
}

public Cursor getAllOil() {
    Cursor c;
    String[] All_Columns = new String[] { "_oil_id", "oil_total_cost",
            "oil_liters", "oil_change_location" };
    c = getWritableDatabase.query(TABLE_OIL, All_Columns, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
}

这是abcmain.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:background="#EDEDED" >

<ListView
    android:id="@+id/list_oil"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:dividerHeight="10dp"
    android:drawSelectorOnTop="true"
    android:footerDividersEnabled="false"
    android:padding="10dp"
    android:scrollbarStyle="outsideOverlay" />

   </RelativeLayout>

这是abclist_group.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:background="@color/coffee"
android:descendantFocusability="blocksDescendants" >

<RelativeLayout
    android:id="@+id/layout_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/oilliters"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#f9f93d"
        android:textSize="17dp" />

    <TextView
        android:id="@+id/oillocation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="29dp"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#f9f93d"
        android:textSize="17dp" />

    <TextView
        android:id="@+id/oildate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/oillocation"
        android:layout_below="@+id/oillocation"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#f9f93d"
        android:textSize="17dp" />

    <TextView
        android:id="@+id/oilcost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/oildate"
        android:layout_below="@+id/oildate"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="#f9f93d"
        android:textSize="17dp" />
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/layout_item"
    android:background="@color/black" />

</RelativeLayout>

它给出的错误是

12-10 18:31:13.690: E/AndroidRuntime(1349): FATAL EXCEPTION: main
12-10 18:31:13.690: E/AndroidRuntime(1349): Process: com.munawwar.sultan, PID: 1349
12-10 18:31:13.690: E/AndroidRuntime(1349): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.munawwar.sultan/com.munawwar.sultan.cursoradapter.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.os.Looper.loop(Looper.java:136)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invoke(Method.java:515)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at dalvik.system.NativeStart.main(Native Method)
12-10 18:31:13.690: E/AndroidRuntime(1349): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.widget.CursorAdapter.init(CursorAdapter.java:172)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at com.munawwar.sultan.cursoradapter.MainActivity.populateListview(MainActivity.java:40)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at com.munawwar.sultan.cursoradapter.MainActivity.onCreate(MainActivity.java:26)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.Activity.performCreate(Activity.java:5231)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-10 18:31:13.690: E/AndroidRuntime(1349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-10 18:31:13.690: E/AndroidRuntime(1349):     ... 11 more

2 个答案:

答案 0 :(得分:1)

将方法更改为:

public Cursor getAllOil() {
    Cursor c;
    db = this.getReadableDatabase();
    String[] All_Columns = new String[] { "_oil_id", "oil_total_cost",
            "oil_liters", "oil_change_location" };
    c = db.query(TABLE_OIL, All_Columns, null, null, null, null, null);
    if (c != null) {
        c.moveToNext();
    }
    return c;
}

在MainActivity中有一个SQLiteDatabase数据库是没有意义的,因为你没有对它做任何事情。

编辑:_id列问题: 请在此处阅读CursorAdapter:http://developer.android.com/reference/android/widget/CursorAdapter.html

&#34; Cursor必须包含一个名为&#34; _id&#34;的列。或者这个课程不起作用&#34;

答案 1 :(得分:0)

原因是因为在这行中你在DBAdapter.java中初始化游标:

c = db.query(TABLE_OIL, All_Columns, null, null, null, null, null);

您的SQLiteDatabase实例仍为null。将该行替换为:

c = getWritableDatabase().query(TABLE_OIL, All_Columns, null, null, null, null, null);

这应解决NullPointerException。