繁体   English   中英

SimpleCursorAdapter在ListActivity中不起作用,日志中没有错误

[英]SimpleCursorAdapter not working in ListActivity, no errors in log

我的MainActivity中有一个简单的列表视图,我想用数据库中的数据填充它。 但这是行不通的。 我只是参加了一项白人活动,没有任何名单的迹象。

据我了解,我需要扩展ListActivity或Activity的Activity。 然后获取Listview并设置一个SimpleCursorAdapter对象。

一些教程建议使用如下代码:

setContentView(myListView)

那是为了什么?

首先是(我认为)相关代码:

MainActivity.java

public class MainActivity extends ListActivity {

    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showTasks();
    }
...
...
...
 public void showTasks() {
        SubtaskDbHelper thelper = new SubtaskDbHelper(getApplicationContext());
        SQLiteDatabase db = thelper.getReadableDatabase();

        Cursor c = db.rawQuery("SELECT * FROM task", null);

        CursorAdapter ca = new SimpleCursorAdapter(
                getApplicationContext(),
                R.layout.list_element,
                c,
                new String[] {Task._ID,Task.TITLE,Task.CONTENT, Task.DUEDATE},
                new int[] {R.id.list_item_id, R.id.list_item_title, R.id.list_item_content, R.id.list_item_duedate},
                2 );

        setListAdapter(ca);

        c.close();
        db.close();
    }


}

我还尝试仅通过“活动”扩展该类。

我不知道要执行此操作缺少什么。

activity_main.xml中

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />
</RelativeLayout>

list_element.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_item_id"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_item_title"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_item_content"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_item_duedate"/>

</LinearLayout>

我找到了解决方案。 问题是我在ShowTasks方法中关闭了cursoradapter。

Cursor c = db.rawQuery("SELECT * FROM task", null);
...
...
...
c.close();

只是不要关闭Cursoradapter。

public void showTasks() {
    SubtaskDbHelper thelper = new SubtaskDbHelper(getApplicationContext());
    SQLiteDatabase db = thelper.getReadableDatabase();

    Cursor c = db.rawQuery("SELECT * FROM task", null);

    CursorAdapter ca = new SimpleCursorAdapter(
            getApplicationContext(),
            R.layout.list_element,
            c,
            new String[] {Task._ID,Task.TITLE,Task.CONTENT, Task.DUEDATE},
            new int[] {R.id.list_item_id, R.id.list_item_title, R.id.list_item_content, R.id.list_item_duedate},
            2 );

    setListAdapter(ca);

    // remove this -> c.close();
    db.close();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM