3

誰かが私のカーソルのどこが間違っているかを知ることができます:データベースからのデータは返されません(少なくとも画面は空白です)。それが私の問題だと思います。DDMSは、データベースを開いたり閉じたりすることを示しています。'Cursor databaseCursor=null;'が何であるかわかりません。する必要があります。Thnx !!

アクティビティ:

    private void displayResultList() {

    Cursor databaseCursor = null;

    DomainAdapter databaseListAdapter = new DomainAdapter(this,
            R.layout.list_item, databaseCursor, new String[] { "label",
                    "title", "description" }, new int[] { R.id.label,
                    R.id.listTitle, R.id.caption });
    databaseListAdapter.notifyDataSetChanged();
    this.setListAdapter(databaseListAdapter);
    }

    private void openAndQueryDatabase() {

    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
        extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File dbfile = new File(extStorageDirectory
                + "/Aero-Technologies/flyDroid/dB/flyDroid.db");

        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
        Log.i("tag", "db opened");
        try {
            db.rawQuery("SELECT * FROM AC_list", null);
        } finally {
            if (db != null)
                Log.i("tag", "db closed");
                db.close();
        }
    } else if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {
        Log.i("tag", "SDCard is NOT writable/mounted");
        Alerts.sdCardMissing(this);
    }
    }

私のアダプター:

public class DomainAdapter extends SimpleCursorAdapter{
private Cursor dataCursor;

private LayoutInflater mInflater;

public DomainAdapter(Context context, int layout, Cursor dataCursor, String[] from,
        int[] to) {
    super(context, layout, dataCursor, from, to);
        this.dataCursor = dataCursor;
        mInflater = LayoutInflater.from(context);
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.label);
        holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
        holder.text3 = (TextView) convertView.findViewById(R.id.caption);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    dataCursor.moveToPosition(position);
    int label_index = dataCursor.getColumnIndex("label"); 
    String label = dataCursor.getString(label_index);

    int title_index = dataCursor.getColumnIndex("title"); 
    String title = dataCursor.getString(title_index);

    int description_index = dataCursor.getColumnIndex("discription"); 
    String description = dataCursor.getString(description_index);

    holder.text1.setText(label);
    holder.text2.setText(title);
    holder.text3.setText(description);

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
}
}

改訂:

displayResultListメソッドを次のように変更します(機能しています)。

private void displayResultList() {

    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
        extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
        File dbfile = new File(extStorageDirectory
                + "/Aero-Technologies/flyDroid/dB/flyDroid.db");
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                null);

        Cursor databaseCursor = db.rawQuery("SELECT * FROM AC_list", null);

        DomainAdapter databaseListAdapter = new DomainAdapter(this,
                R.layout.list_item, databaseCursor, new String[] { "label",
                        "title", "description" }, new int[] { R.id.label,
                        R.id.listTitle, R.id.caption });
        databaseListAdapter.notifyDataSetChanged();
        this.setListAdapter(databaseListAdapter);
    } else if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {
        Log.i("tag", "SDCard is NOT writable/mounted");
        Alerts.sdCardMissing(this);
    }
}
4

2 に答える 2

2

私はあなたの問題がにあると思います

try {
    db.rawQuery("SELECT * FROM AC_list", null);
} finally {
    if (db != null)
            Log.i("tag", "db closed");
                db.close();
}

クエリを実行するとすぐに閉じますが、クエリがdatabaseCursorに保存されることはありません。おそらく、次のようにします。

try {
    databaseCursor = db.rawQuery("SELECT * FROM AC_list", null);
} catch(Exception e) {
    if (db != null)
            Log.i("tag", "db closed");
                db.close();

}

これがお役に立てば幸いです

于 2011-04-29T17:42:31.727 に答える
1
Cursor databaseCursor = null;

これは、アダプターに渡すものです。後でクエリを実行していることは確かですが、そのクエリからカーソルの結果をキャプチャしたり、アダプタにカーソルを設定したりすることはありません。したがって、カーソルは常にnullであるため、結果は得られません。

ああ、カーソルが生きている間もデータベースを開いたままにしておく必要があります。データベースを閉じるのではなく、アクティビティでstartManagingCursorメソッドを使用します。

于 2011-04-29T17:53:25.110 に答える