Blank ListView with SimpleCursorAdapter

本小妞迷上赌 提交于 2020-01-06 03:56:25

问题


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;
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/20877864/blank-listview-with-simplecursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!