Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

android - Cursor and Adapter

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...