3

I have created a custom layout for ListFragment, using the default ArrayAdapter. My problem is that the empty message is displayed on each row, when the list is not empty (when the list is empty, the message is displayed only once as expected).

headline_list.xml (i.e. my custom layout file)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <TextView android:id="@+id/my_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView android:id="@id/android:empty"
        android:text="There are no data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> 

</LinearLayout>

HeadlineFragment.java

public class HeadlineFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.headline_list, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<String> headlines = ...;          // populate headlines list

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.headline_list, R.id.my_text, headlines));
    }
}

enter image description here

1 Answer 1

5

You've got your layouts a little mixed up.

new ArrayAdapter<String>(getActivity(),
    R.layout.headline_list, R.id.my_text, headlines)

The second parameter in the ArrayAdapter constructor is meant to be the ID for an individual row's layout, not the entire Fragment layout. The third parameter references a TextView within the row layout to display the item's data.

You can provide your own row layout, or you can use one provided by the SDK. An example of the latter:

new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, headlines)

In this example, android.R.layout.simple_list_item_1 is itself just a TextView, so we don't need to provide the ID for one.

Also, you should consider keeping a reference to the Adapter, should you need to later modify it.


It appears that you mean for the my_text TextView to be your custom row layout. If so, remove it from the Fragment's layout, headline_list, and put it in its own layout file; e.g., list_row.xml. Your layout files would then be as follows:

headline_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="There are no data" /> 

</LinearLayout>

list_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Then you would instantiate your Adapter like so:

new ArrayAdapter<String>(getActivity(), R.layout.list_row, headlines)

Again, since the row layout is just a TextView, we don't need to pass an ID in the constructor for one.

2
  • Thanks for your answer. The built-in layout works fine, but I am asking about the custom layout. Looking at your answer, you write that the 2nd par is the ID for an individual row's layout. What does it mean in practice? See the xml file I have posted. Note that the API reference says that the 2nd par is The resource ID for a layout file containing a layout to use when instantiating views
    – perissf
    Commented Dec 20, 2014 at 21:48
  • 1
    The layout you've posted is the Fragment's entire layout - a LinearLayout containing the ListView and the two TextViews. The layout ID you provide to the Adapter's constructor is for the individual rows displayed within the ListView; the "layout to use when instantiating views", i.e., the rows. Since you've given the Adapter the Fragment's layout, each of the rows in your image is actually a LinearLayout with an empty (invisible) ListView and two TextViews, one of which is your empty View, which is why it's showing in every row.
    – Mike M.
    Commented Dec 20, 2014 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.