SimpleCursorAdapter和ListView应用程序崩溃

时间:2014-03-09 10:35:01

标签: android sqlite listview crash simplecursoradapter

我在listview中显示来自db的记录时遇到问题。当我启动我的应用程序时,它会在开始时崩溃。这是我的代码:

dbHelper = new DbAdapter(MainActivity.this);
dbHelper.open();
cursor = dbHelper.fetchAllContacts();


startManagingCursor(cursor);


String[] columns = new String[] {
DbAdapter.KEY_NAME,


 DbAdapter.KEY_SURNAME,
         DbAdapter.KEY_SEX, 
         DbAdapter.KEY_BIRTH_DATE
       };


       int[] to = new int[] { 

         R.id.name,
         R.id.cognome,
         R.id.sesso,
         R.id.datanascita,
       };


       SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
         MainActivity.this,  R.layout.contatti, 
         cursor, 
         columns, 
         to,
         0);

       ListView listView = (ListView) findViewById(R.id.listView1);

       listView.setAdapter(dataAdapter);          

 cursor.close();
 dbHelper.close();

这是我对listview的布局:

contatti.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Nome : "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Cognome : "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="Sesso : "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="Data nasc. : "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:text="TextView" />

    <TextView
        android:id="@+id/cognome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/continent"
        android:text="TextView" />

    <TextView
        android:id="@+id/sesso"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:text="TextView" />

    <TextView
        android:id="@+id/datanascita"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/name"
        android:text="TextView" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您应该尝试在cursor.close()上不使用cursor and DBHelper的情况下运行您的应用。
SimpleCursorAdapter需要将其打开,以便管理列表视图。