[英]SimpleCursorAdapter not working in ListActivity, no errors in log
I have a simple listview in my MainActivity that I want to populate with Data form my Database. 我的MainActivity中有一个简单的列表视图,我想用数据库中的数据填充它。 But it doesn't work. 但这是行不通的。 I just get a white activity with no signs of any list. 我只是参加了一项白人活动,没有任何名单的迹象。
As far as I understand it I need my Activity that extends ListActivity or Activity. 据我了解,我需要扩展ListActivity或Activity的Activity。 Then get the Listview and set a SimpleCursorAdapter object. 然后获取Listview并设置一个SimpleCursorAdapter对象。
Some tutorials suggest a line of code that goes like this: 一些教程建议使用如下代码:
setContentView(myListView)
What would that be for? 那是为了什么?
First here is the (i think-) relevant code: 首先是(我认为)相关代码:
MainActivity.java 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();
}
}
I also tried to extend the class just with "Activity". 我还尝试仅通过“活动”扩展该类。
I do not know what I am missing to get this running. 我不知道要执行此操作缺少什么。
activity_main.xml 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 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>
I found the solution. 我找到了解决方案。 The problem was that I closed the cursoradapter in the ShowTasks method. 问题是我在ShowTasks方法中关闭了cursoradapter。
Cursor c = db.rawQuery("SELECT * FROM task", null);
...
...
...
c.close();
Just do not close the Cursoradapter. 只是不要关闭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.