光标和适配器

发布于 2024-11-04 06:50:59 字数 4326 浏览 5 评论 0原文

有人能看到我的光标有什么问题吗:未返回数据库中的数据(至少屏幕是空白的)。我认为这是我的问题。在DDMS中显示它打开&关闭数据库。我不确定'Cursor databaseCursor = null;'是什么需要。谢谢!!

活动:

    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);
    }
}

can someone see what I have wrong with my cursor: The data from the db is not returned (at least the screen is blank). I think that is my problem. In the DDMS shows it opens & closes the db. I am not sure what 'Cursor databaseCursor = null;' needs to be. Thnx!!

The Activity:

    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);
    }
    }

My Adapter:

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;
}
}

REVISED:

Change the displayResultList method to the following (working):

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-11-11 06:50:59

我认为你的问题在于

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();

}

我希望这有帮助

I think your problem is in

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

you are closing it as soon as you query and your query is never saved into databaseCursor, maybe what you wanted to do was:

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

}

I hope this helps

留蓝 2024-11-11 06:50:59
Cursor databaseCursor = null;

这就是您传递给适配器的内容。当然,您稍后会进行查询,但是您从未捕获该查询的游标结果,也没有在适配器上设置游标。因此,由于游标始终为空,因此您永远不会有结果。

哦,您还需要在光标处于活动状态时保持数据库打开。对活动使用 startManagingCursor 方法,而不是关闭数据库。

Cursor databaseCursor = null;

This is what you are passing to the adapter. Sure you are querying later, but you are never capturing the cursor result from that query, nor are you setting the cursor on the adapter. So since cursor is always null, you never have results.

Oh, you also need to keep the db open while your cursor is alive. Use the startManagingCursor method on the activity rather than close the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文