Search code examples
androidlistviewsimplecursoradapter

CursorAdapter not displaying elements in ListView


I am trying to get a column of SQLITE database to a listview using a SimpleCursorColumn. I am getting the accurate number of rows in the list view, but the text is not appearing in the list. I printed out the content of the cursor and it contains the correct elements.

Why is the text not showing up in the list? Surely it must be easy that I am being stupid about.

        public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int LOADER_ID = 42;
    private CursorAdapter _adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3_frag, container, false);

        ListView lv = view.findViewById(R.id.student_view);
        _adapter = new SimpleCursorAdapter(getContext(),
                R.layout.container_list_item_view, null,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
                new int[] { R.id.list_item });
        lv.setAdapter(_adapter);
        getLoaderManager().initLoader(LOADER_ID, null, this);

        return view;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != LOADER_ID) {
            return null;
        }
        return new CursorLoader(getContext(),
                Contract.Student_Table.CONTENT_URI,
                new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        _adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        _adapter.swapCursor(null);
    }
}

tab3_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#c2f6ff"
    android:orientation="vertical">

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

    <TextView
        android:id="@+id/student_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>

Solution

  • Better use loader. Try below example:

    public class LanguageListActivity extends ListActivity implements
            LoaderCallbacks<Cursor> {
        private static final int LOADER_ID = 42;
        private CursorAdapter _adapter;
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.container_list);
            _adapter = new SimpleCursorAdapter(this,
                    R.layout.container_list_item_view, null,
                    new String[] { DatabaseConstants.COL_LANG_NAME },
                    new int[] { R.id.list_item });
            setListAdapter(_adapter);
            getLoaderManager().initLoader(LOADER_ID, null, this);
        }
    
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            if (id != LOADER_ID) {
                return null;
            }
            return new CursorLoader(LanguageListActivity.this,
                    LanguageContentProvider.CONTENT_URI,
                    new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
                    null);
        }
    
        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            _adapter.swapCursor(data);
        }
    
        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            _adapter.swapCursor(null);
      }
    }
    

    container_list.xml:

     <?xml version="1.0" encoding="utf-8"?>
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    

    container_list_item_view.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="2dip" >
    
        <TextView
            android:id="@+id/list_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dip" />
    
    </LinearLayout>