空白的ListView与SimpleCursorAdapter [英] Blank ListView with SimpleCursorAdapter
问题描述
我经历了不少类似的问题,看起来在这里所以没有它,但似乎解决了我的。所以,我想获得一个ListActivity使用SimpleCursorAdapter交谈的SQLite。我已经运行通过调试的code,一切看起来不错,但我的ListView是空的。有一个在DB肯定的东西,我已注销的是,其中至少有列适量光标。
我是相当新的Android开发人员,我很清楚地知道,我可能失去了一些东西真的很明显,但如果你能指出来,将是巨大的。
下面是我的活动
公共类ListItemsActivity扩展ListActivity { 私人RevisionItemsDataSource数据源; 私人SimpleCursorAdapter itemAdapter;
私人按钮Add按钮; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 数据源=新RevisionItemsDataSource(本);
datasource.open(); 的setContentView(R.layout.revision_list); 光标光标= datasource.fetchAll();
的String [] =列新的String [] {
MySQLiteHelper.COLUMN_QUESTION,
MySQLiteHelper.COLUMN_ANSWER
}; INT []为= INT新[] {
R.id.questionText,
R.id.answerText
}; itemAdapter =新SimpleCursorAdapter(
对此,R.layout.revision_item_view,
光标,列,
到,0); this.setListAdapter(itemAdapter);
}
}
这是清单XML
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < ListView控件
机器人:ID =@机器人:ID /列表
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =0dip
机器人:layout_weight =1/> <按钮
机器人:ID =@ + ID / addItemButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =CENTER_HORIZONTAL
机器人:文字=添加/>
< / LinearLayout中>
和XML的单项格式
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / questionText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=大文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge? <的TextView
机器人:ID =@ + ID / answerText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=TextView的/>
< / LinearLayout中>
和最终的数据源对象
公共类RevisionItemsDataSource { 私有静态最后弦乐DB_TAG =数据库; //数据库字段
私人SQLiteDatabase数据库;
私人MySQLiteHelper dbHelper;
私有String [] = allColumns {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_QUESTION,
MySQLiteHelper.COLUMN_ANSWER
}; 公共RevisionItemsDataSource(上下文的背景下){
dbHelper =新MySQLiteHelper(背景);
} 公共RevisionItemsDataSource的open()抛出的SQLException {
数据库= dbHelper.getWritableDatabase();
返回此;
} 公共光标使用fetchall(){ 光标光标= database.query(MySQLiteHelper.TABLE_ITEMS,
allColumns,NULL,NULL,NULL,NULL,NULL); 如果(指针!= NULL){
cursor.moveToFirst();
} 返回游标;
}
}
好吧虚惊一场......你知道我说,有在DB的东西,有没有。我已经重新建立了我的模拟器,因此核爆数据库吧。
我正式马上拍着自己。
I have looked through quite a few similar issues here on SO but none of which seem to solve mine. So I am trying to get a ListActivity to talk to SQLite using the SimpleCursorAdapter. I have run the code through the debugger and everything looks okay but my listView is empty. There is definately stuff in the DB and I have logged out what is in the cursor which has the right amount of columns at least.
I'm fairly new to Android dev and I'm well aware that I might be missing something really obvious but if you could point it out that would be great.
Below is my Activity
public class ListItemsActivity extends ListActivity {
private RevisionItemsDataSource datasource;
private SimpleCursorAdapter itemAdapter;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datasource = new RevisionItemsDataSource(this);
datasource.open();
setContentView(R.layout.revision_list);
Cursor cursor = datasource.fetchAll();
String[] columns = new String[] {
MySQLiteHelper.COLUMN_QUESTION,
MySQLiteHelper.COLUMN_ANSWER
};
int[] to = new int[] {
R.id.questionText,
R.id.answerText
};
itemAdapter = new SimpleCursorAdapter(
this, R.layout.revision_item_view,
cursor, columns,
to, 0);
this.setListAdapter(itemAdapter);
}
}
This is the list XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:id="@+id/addItemButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add" />
</LinearLayout>
And the XML for the single item formatting
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/questionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/answerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
And finally the Datasource object
public class RevisionItemsDataSource {
private static final String DB_TAG = "DATABASE";
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_QUESTION,
MySQLiteHelper.COLUMN_ANSWER
};
public RevisionItemsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public RevisionItemsDataSource open() throws SQLException {
database = dbHelper.getWritableDatabase();
return this;
}
public Cursor fetchAll() {
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
allColumns, null, null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
Okay false alarm... you know I said that there was stuff in the DB, there wasn't. I had re-created my emulator and therefore nuked the DB with it.
I'm officially slapping myself right now.
这篇关于空白的ListView与SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!