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_UN开发者_StackOverflow社区MOUNTED)) {
Log.i("tag", "SDCard is NOT writable/mounted");
Alerts.sdCardMissing(this);
}
}
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
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.
精彩评论