有人可以看到我的光标错误:不返回数据库中的数据(至少屏幕为空白)。我认为这是我的问题。在DDMS显示它打开&关闭数据库。我不确定'Cursor databaseCursor = null;'需要是。日Thnx !!
活动:
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;
}
}
REVISED:
将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);
}
}
答案 0 :(得分:2)
我认为你的问题在
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();
}
我希望这会有所帮助
答案 1 :(得分:1)
Cursor databaseCursor = null;
这是您传递给适配器的内容。当然你以后会查询,但你从不从该查询中捕获游标结果,也没有在适配器上设置游标。因此,由于光标始终为空,因此您永远不会有结果。
哦,你还需要在光标处于活动状态时保持数据库打开。在活动上使用startManagingCursor方法,而不是关闭数据库。