Android SQLiteDatabase返回null(空白)行

时间:2012-04-11 20:30:51

标签: android

用我正在尝试构建的应用程序拉出我的头发。我已经尝试了很多不同的东西,书籍,方式,youtube教程,我已经失去了数!我正在构建一个简单的应用程序,显示飞机的图片,然后用户单击将其添加到学习列表(已验证通过使用toast而不是listview工作),然后单击另一个按钮以查看此列表。

我让应用程序返回正确的行数,但它们都是空白的!从我所看到的这意味着行是空的,但我不能为我的生活图出什么是错的!无论谁能帮助我,我都会永远负债:)。

谢谢!这是代码......

(数据库助手/适配器)

    package com.atcapp;

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

public class DatabaseAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
private static final String TAG = "DatabaseAdapter";

private static final String DATABASE_NAME = "LearntListdb";
private static final String DATABASE_TABLE = "AircraftTable";

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table AircraftTable (_id integer primary key         autoincrement, "
        + "name text not null);";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DatabaseAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + "which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS AircraftNames");
        onCreate(db);
    }
}

// ---opens the database---
public DatabaseAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert an aircraft into the database---
public long insertAircraft(String name) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// ---retrieves all the list---
public Cursor getAllAircraft() {

    Cursor c = db.query(DATABASE_TABLE, new String[] {"_id", "name"}, null, null, null, null, null);
    return c;
}

}

(列出活动)

    package com.atcapp;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class DataView extends ListActivity {

private DatabaseAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.aircraftlist);

    mDbHelper = new DatabaseAdapter(this);

    mDbHelper.open();
    fillData();

}

private void fillData(){
    Cursor c = mDbHelper.getAllAircraft();

    startManagingCursor(c);



    String[] from = new String[] {DatabaseAdapter.KEY_ROWID, DatabaseAdapter.KEY_NAME};
    int[] to = new int[] {android.R.id.list};

    SimpleCursorAdapter myList =
            new SimpleCursorAdapter(this, R.layout.list_row, c, from, to);
    setListAdapter(myList);

}

}

1 个答案:

答案 0 :(得分:1)

您需要在数组中提供textview id。您需要将数据从光标映射到适配器中的视图。

查看list_row布局并使用textview id。