Reputation: 11
I have a problem displaying an records from my db in listview. When i start my app it crash at the beggining.This is my code :
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();
This is the my layout for the 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>
Upvotes: 1
Views: 444
Reputation: 168
You should try running your app without using cursor.close()
on the cursor and DBHelper
.
The SimpleCursorAdapter
needs it to be open so it can manage the list view.
Upvotes: 1