javalistviewandroid-cursoradapter

Android ResourceCursorAdapter: Only one item displayed in ListView


I'm working on an adapter that's supposed to display a list of items derived from an SQLite database. The ListView is embedded in a Fragment, populated in a custom adapter, sub-classed from a ResourceCursorAdapter. Iterating over the cursor and printing its content shows me that it contains a number of entries. However, bindView only gets called for the first entry, being the only one that's displayed in the ListView. What am I doing wrong?

This is the (stripped down) code:

Fragment XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/network_settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false">

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

</LinearLayout>

Adapter list item XML

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

    <TextView
        android:id="@+id/remote_ip_address"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.45"/>

    <TextView
        android:id="@+id/remote_port"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.25"/>

    <TextView
        android:id="@+id/address_protocol"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.2"/>

    <ImageButton
        android:id="@+id/delete_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/delete_address"
        android:src="@drawable/close"/>

</LinearLayout>

Fragment class (simplified, only containing onCreateView as that's where the adapter gets instanciated. The fragment itself works fine)

public class NetworkSettingsFragment extends Fragment {

    private View mView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.network_settings, container, false);

        final SQLiteDatabase db = mActivity.getDatabase();
        final ListView addressesList = mView.findViewById(R.id.addresses_list);

        final String[] addrFields = new String[]{
            SettingsContract.AddressSettingsEntry._ID,
            SettingsContract.AddressSettingsEntry.IP_ADDRESS,
            SettingsContract.AddressSettingsEntry.PORT,
            SettingsContract.AddressSettingsEntry.PROTOCOL
        };
        final String sortOrder = SettingsContract.AddressSettingsEntry.IP_ADDRESS + " DESC";

        final Cursor addressesCursor = db.query(
            SettingsContract.AddressSettingsEntry.TABLE_NAME,
            addrFields,
            null,
            null,
            null,
            null,
            sortOrder
        );

        final AddressesListAdapter addressesListAdapter = new AddressesListAdapter(
            getActivity(), R.layout.address_list_item, addressesCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
        );
        addressesList.setAdapter(addressesListAdapter);
    }
}

The adapter

public class AddressesListAdapter extends ResourceCursorAdapter {

    final private static String TAG = "AddressesListAdapter";
    private int mLayout;

    public AddressesListAdapter(Context context, int layout, Cursor c, int flags) {
        super(context, layout, c, flags);
        mLayout = layout;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(mLayout, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.d(TAG, "the view: " + view + "\ncursor: " + cursor.getPosition() + "\nnum items: " + cursor.getCount());

        TextView ipText = view.findViewById(R.id.remote_ip_address);
        TextView portText = view.findViewById(R.id.remote_port);
        TextView protocolText = view.findViewById(R.id.address_protocol);

        final long id = cursor.getLong(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry._ID));
        final String ip = cursor.getString(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.IP_ADDRESS));
        final int port = cursor.getInt(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.PORT));
        final String protocol = cursor.getString(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.PROTOCOL));

        ipText.setText(ip);
        portText.setText(String.valueOf(port));
        protocolText.setText(protocol);
    }
}

Solution

  • To answer my own question: It finally turned out to be a layout problem. What I hadn't considered was that my Fragment within which my ListView gets created within a ScrollView (which was not part of the layout xml that I posted in my question. Within that ScrollView a list of choices (links to various settings) gets replaced by my Fragment containing the ListView. However, the original ScrollView doesn't necessarily fill the full height of the screen, so I just needed to set an attribute android:fillViewport to the ScrollView's XML and set it to true. That allowed the ListView to stretch to the bottom of the screen and the items, of which I first thought they weren't considered by my adapter, suddenly became visible.

    Here's the simplified source of the parent ScrollView (within a FrameLayout)

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/settings_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorDarkTransparentBackground">
    
        <ScrollView
            android:id="@+id/settings_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingStart="20dp"
            android:paddingLeft="20dp"
            android:paddingTop="5dp"
            android:paddingEnd="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="20dp"
            android:fillViewport="true"
            android:visibility="visible" />
    
    </FrameLayout>