光标和适配器 [英] Cursor and Adapter
问题描述
有人可以看到我有什么错我的光标:不返回从数据库中的数据(至少画面是空白)。我觉得这是我的问题。在DDMS显示它打开和放大器;关闭分贝。我不知道是什么光标databaseCursor = NULL;'需要。日Thnx!
该活动:
私人无效displayResultList(){
光标databaseCursor = NULL;
DomainAdapter databaseListAdapter =新DomainAdapter(这一点,
R.layout.list_item,databaseCursor,新的String [] {标签,
标题,说明},新的INT [] {R.id.label,
R.id.listTitle,R.id.caption});
databaseListAdapter.notifyDataSetChanged();
this.setListAdapter(databaseListAdapter);
}
私人无效openAndQueryDatabase(){
如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_MOUNTED)){
。extStorageDirectory = Environment.getExternalStorageDirectory()的toString();
文件DBFILE =新的文件(extStorageDirectory
+/Aero-Technologies/flyDroid/dB/flyDroid.db);
SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);
Log.i(DB开,标签);
尝试 {
db.rawQuery(SELECT * FROM AC_list,NULL);
} 最后 {
如果(DB!= NULL)
Log.i(标签,DB封闭);
db.close();
}
}否则,如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_UNMOUNTED)){
Log.i(标签,SD卡不可写/安装);
Alerts.sdCardMissing(本);
}
}
我的适配器:
公共类DomainAdapter扩展SimpleCursorAdapter {
私人光标dataCursor;
私人LayoutInflater mInflater;
公共DomainAdapter(上下文的背景下,INT布局,光标dataCursor,字符串[]而成,
INT []键){
超(背景下,布局,dataCursor,从,到);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(上下文);
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.list_item,NULL);
持有人=新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(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
dataCursor.moveToPosition(位置);
INT label_index = dataCursor.getColumnIndex(标签);
字符串标签= dataCursor.getString(label_index);
INT title_index = dataCursor.getColumnIndex(标题);
字符串标题= dataCursor.getString(title_index);
INT description_index = dataCursor.getColumnIndex(详细描述);
字符串描述= dataCursor.getString(description_index);
holder.text1.setText(标签);
holder.text2.setText(职称);
holder.text3.setText(介绍);
返回convertView;
}
静态类ViewHolder {
TextView的文本1;
TextView的文本2;
TextView的文本3;
}
}
修订版:
更改displayResultList方法如下(工作):
私人无效displayResultList(){
如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_MOUNTED)){
extStorageDirectory = Environment.getExternalStorageDirectory()
的ToString();
文件DBFILE =新的文件(extStorageDirectory
+/Aero-Technologies/flyDroid/dB/flyDroid.db);
SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,
空值);
光标databaseCursor = db.rawQuery(SELECT * FROM AC_list,NULL);
DomainAdapter databaseListAdapter =新DomainAdapter(这一点,
R.layout.list_item,databaseCursor,新的String [] {标签,
标题,说明},新的INT [] {R.id.label,
R.id.listTitle,R.id.caption});
databaseListAdapter.notifyDataSetChanged();
this.setListAdapter(databaseListAdapter);
}否则,如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_UNMOUNTED)){
Log.i(标签,SD卡不可写/安装);
Alerts.sdCardMissing(本);
}
}
我觉得你的问题是在
尝试{
db.rawQuery(SELECT * FROM AC_list,NULL);
} 最后 {
如果(DB!= NULL)
Log.i(标签,DB封闭);
db.close();
}
你,只要你查询关闭它和您的查询永远不会保存到databaseCursor,你想做的事,也许是什么:
尝试{
databaseCursor = db.rawQuery(SELECT * FROM AC_list,NULL);
}赶上(例外五){
如果(DB!= NULL)
Log.i(标签,DB封闭);
db.close();
}
我希望这有助于
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);
}
}
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
这篇关于光标和适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!