Search code examples
androidandroid-listviewsimplecursoradapter

ListView + SimpleCursorAdapter doesn't display data


Ok i know that this question have been asked a lot of times here and i have tried all the solutions but none seem to work. I am following the learning android book and i am trying to display the data from the database in a form of ListView using SimpleCursorAdapter to plug it in. The screen which is supposed to show the data doesn't show anything except for the title.

package com.example.yamba;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TimelineActivity extends Activity {
    static final String[] FROM = { StatusData.C_USER,StatusData.C_TEXT, StatusData.C_CREATED_AT};
    static final int[]TO = {R.id.text_user,R.id.text_text,R.id.text_createdAt};

    ListView list;
    Cursor cursor;
    SimpleCursorAdapter adapter;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.timeline);
        list = (ListView) findViewById(R.id.list);


        adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
        list.setAdapter(adapter);

        cursor = ((YambaApp) getApplication()).statusData.query();
//      while (cursor.moveToNext()) {
//          String user = cursor.getString(cursor.getColumnIndex(StatusData.C_USER));
//          String text = cursor.getString(cursor.getColumnIndex(StatusData.C_TEXT));
//          list.append(String.format("\n%s: %s", user, text));
//      }
    }
}



***Timeline.xml***

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/my_blue"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/timeline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/Timeline" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </ListView>
    <!--
<ScrollView 
         android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/timelineView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</ScrollView>
    -->

</LinearLayout>



***row.xml***

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Slashdot"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/text_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/text_createdAt"
        android:text="A really long example tweet for just test purposes"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/text_createdAt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/text_user"
        android:layout_alignBottom="@+id/text_user"
        android:layout_alignParentRight="true"
        android:text="10 minutes ago"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

I have pasted my code above. Can anyone tell me what is messing up the display? Really appreciate the help

Thanks!!


Solution

  • The cursor needs to be created before you create the SimpleCursorAdapter, otherwise what are you passing in to the constructor. Here's a rewrite:

    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.timeline);
    list = (ListView) findViewById(R.id.list);
    
    
    cursor = ((YambaApp) getApplication()).statusData.query();
    adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
    list.setAdapter(adapter);