setEmptyView和ListView

时间:2016-02-07 18:13:47

标签: android listview

我有问题。我想这样做,如果列表为空,则创建EmptyView,否则如果列表包含项目,则使用列表中的元素创建ListView。 怎么做?

以下是代码:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
                  .............
        CrimeAdapter adapter = new CrimeAdapter(mCrimes);
        setListAdapter(adapter);
                  .............
}

@TargetApi(11)
@Override
public ListView onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) 
{

        ListView view = (ListView)parent.findViewById(R.id.list);
        view.setEmptyView(parent.findViewById(R.id.empty));
                ................        
        return view;
}

private class CrimeAdapter extends ArrayAdapter<Crime> 
{
    public CrimeAdapter(ArrayList<Crime> crimes) 
    {
        super(getActivity(), 0, crimes);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = getActivity().getLayoutInflater()
                .inflate(R.layout.list_item_crime, null);
        }

        Crime c = getItem(position);

        TextView titleTextView =
            (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
        titleTextView.setText(c.getTitle());
        TextView dateTextView =
            (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);
        dateTextView.setText(c.getDate().toString());
        CheckBox solvedCheckBox =
            (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
        solvedCheckBox.setChecked(c.isSolved());

        return convertView;
    }
}

因为有xml文件(list_and_empty_view.xml),它包含ListView for list和View for emptyView。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

        </ListView>

        <View
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/is_empty" />

</FrameLayout>

2 个答案:

答案 0 :(得分:0)

你的EmptyView应该有这样的id:

<TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/empty"// id should be set like this.
        android:text="No Items Found"/>

然后您需要在致电listView.setEmptyView()后致电listView.setAdapter()。 并使用setEmptyView(findViewById(android.R.id.empty)

答案 1 :(得分:0)

根据ListFragment documentation,您必须使用ID "@android:id/list""@android:id/empty"。 (请注意,文档中的后面有一个拼写错误。)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

        </ListView>

        <View
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/is_empty" />

</FrameLayout>