android-光标和适配器

android-光标和适配器,第1张

概述有人可以看到我的光标有什么问题吗:数据库中的数据没有返回(至少屏幕是空白的).我认为那是我的问题.在DDMS中显示它已打开&关闭数据库.我不确定’CursordatabaseCursor=null;’需要是.谢谢!活动:privatevoiddisplayResultList(){CursordatabaseCursor=null;

有人可以看到我的光标有什么问题吗:数据库中的数据没有返回(至少屏幕是空白的).我认为那是我的问题.在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);    }}

解决方法:

我认为您的问题出在

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

我希望这有帮助

总结

以上是内存溢出为你收集整理的android-光标和适配器全部内容,希望文章能够帮你解决android-光标和适配器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071394.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存