Android 带有SimpleCursorAdapter的空白列表视图
我在这里已经研究了很多类似的问题,但似乎没有一个能解决我的问题。因此,我尝试使用SimpleCursorAdapter获取一个ListActivity来与SQLite对话。我已经通过调试器运行了代码,看起来一切正常,但我的listView是空的。数据库中肯定有东西,我已经注销了游标中的东西,它至少有适当数量的列 我是Android开发的新手,我很清楚我可能遗漏了一些非常明显的东西,但如果你能指出这一点,那就太好了 下面是我的活动Android 带有SimpleCursorAdapter的空白列表视图,android,sqlite,listview,simplecursoradapter,Android,Sqlite,Listview,Simplecursoradapter,我在这里已经研究了很多类似的问题,但似乎没有一个能解决我的问题。因此,我尝试使用SimpleCursorAdapter获取一个ListActivity来与SQLite对话。我已经通过调试器运行了代码,看起来一切正常,但我的listView是空的。数据库中肯定有东西,我已经注销了游标中的东西,它至少有适当数量的列 我是Android开发的新手,我很清楚我可能遗漏了一些非常明显的东西,但如果你能指出这一点,那就太好了 下面是我的活动 public class ListItemsActivity ex
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);
}
}
这是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>
好的假警报。。。你知道我说过数据库里有东西,没有。我重新创建了仿真器,因此用它来控制数据库 我现在正式打了自己一巴掌
<?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>
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;
}
}