I have a simple listview in my MainActivity that I want to populate with Data form my Database. 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. Then get the Listview and set a SimpleCursorAdapter object.
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
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
<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>
I found the solution. The problem was that I closed the cursoradapter in the ShowTasks method.
Cursor c = db.rawQuery("SELECT * FROM task", null);
...
...
...
c.close();
Just do not close the 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.