ListFragment with custom layout: always showing blank message
I created my own layout for ListFragment
using the default ArrayAdapter
. My problem is that a blank message is displayed on each line 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 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));
}
}
source to share
Your layouts are a little messed up.
new ArrayAdapter<String>(getActivity(),
R.layout.headline_list, R.id.my_text, headlines)
The second parameter in the constructor ArrayAdapter
should be an identifier for the individual row layout, not the entire layout Fragment
. The third parameter refers to TextView
in the row layout to display the item data.
You can provide your own row layout, or you can use the 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
, itself is simple TextView
, so we don't need to provide an ID for it.
In addition, you should consider keeping a reference to the adapter if you need to modify it later.
It looks like you mean my_text
TextView
your own row layout. If so, remove it from the layout Fragment
, headline_list
and place it in your own layout file; eg list_row.xml
. Your layout files will look like this:
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 have to create your adapter like this:
new ArrayAdapter<String>(getActivity(), R.layout.list_row, headlines)
Again, since string layout is simple TextView
, we don't need to pass an id in the constructor for one.
source to share