[英]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.