Question

I wrote the following classes to create a list view with custom list rows. I use the Android Support Library v4 for the project and the ActionBarSherlock library to integrate an actionbar for older devices.

public class CustomListActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Sherlock___Theme_DarkActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_list);
    }
}

...

public class ListFragment extends SherlockListFragment implements LoaderCallbacks<Cursor> {
    private Activity mActivity;
    private CursorAdapter mAdapter;
    // Query parameter as members ...
    private String mFromColumns;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText("No data to display");
        mActivity = getActivity();
        // Query parameters are stored in members here ...
        mFromColumns = { "_id", "name" };
        mAdapter = new CustomCursorAdapter(mActivity, null, 0);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
        return new CursorLoader(mActivity, mUri, mFromColumns, mSelection, mSelectionArgs, sortOrder);
    }

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

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

...

public class CustomCursorAdapter extends CursorAdapter {
    private LayoutInflater mInflater;

    public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView listItem = (TextView)view.findViewById(R.id.name);
//      TextView listItem = (TextView)view.findViewById(android.R.id.text1);
        String text = cursor.getString(cursor.getColumnIndex("name"));
        listItem.setText(text);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.list_item, parent, false);
//      return mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    }
}

The list view successfully loads and displays the data rows, when I use the list item and layout provided by the framework (uncommented lines in CustomCursorAdapter). However, when I swap the lines to use my custom layout and list item, findViewById returns null. Here are the xml files.

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.example.app.fragment.ListFragment"
    android:id="@+id/list_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</fragment>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >
    <TextView 
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

I am pretty sure, the problem are how I link the xml files or how they are build. I hope you can see the error. Actually, I do not understand how list_item.xml is used as the row layout for the list view. The only reference I set to fragment_list.xml is via setContentView(R.layout.fragment_list);. And this xml file does not contain any container element such as <ListView android:id="@android:id/list" /> (which I saw in other setups).

Additionally:

I would like to know if it is still recommended to use the ViewHolder pattern in a CursorAdapter or if this behavior is built-in already. I saw it several times in other examples but those inherited from BaseAdapter or SimpleCursorAdapter.


Lessons learned:

While I was trying to solve the problem, I had the right setting for the XML files somewhen. But another thing hindered me from gettings things running. Here is what you need to be aware of.
If you want to display multiple information in your row layout it is important that you include the associated column names in the fromColumn parameter of your CursorLoader. Otherwise, you will run into an exception:

java.lang.IllegalStateException: get field slot from row 0 col -1 failed

Every column name in fromColumns ...

String[] fromColumns = { "_id", "name", "comments" }

... is related to what you ask the cursor for. The _id column is mandatory to use the cursor as an iterator.

String name = cursor.getString(cursor.getColumnIndex("name"));
String comment = cursor.getString(cursor.getColumnIndex("comment"));
Was it helpful?

Solution

Your code works in both cases for me (as is and when I change line comments). The only thing: I've added public empty constructor to ListFragment.

And it is still recommended to use ViewHolder pattern with CursorAdapter.

public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        holder.name = (TextView) view.findViewById(R.id.name);

        view.setTag(holder);
    }

    String text = cursor.getString(cursor.getColumnIndex("name"));
    holder.name.setText(text);
}

private static class ViewHolder {
    TextView name;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top