android-光标和适配器

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

}

我希望这有帮助

上一篇:使用npm install安装前端项目依赖时报错


下一篇:我如何访问存在于ItemTemplate中的ServerControl?