2014-12-11 5 views
1

У меня есть простой список в моей MainActivity, который я хочу заполнить формой данных моей базы данных. Но это не сработает. Я просто получаю белую активность без каких-либо признаков.SimpleCursorAdapter не работает в ListActivity, никаких ошибок в журнале

Насколько я понимаю, мне нужна моя деятельность, которая расширяет ListActivity или Activity. Затем получите Listview и установите объект SimpleCursorAdapter.

Некоторые учебники предлагают строку кода, которая идет как это:

setContentView(myListView) 

Что бы это было для?

Во-первых здесь есть (я think-) соответствующий код:

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> 

ответ

0

Я нашел решение. Проблема заключалась в том, что я закрыл курсорный указатель в методе ShowTasks.

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(); 
} 
Смежные вопросы