开发者

Setting a layout to use as empty view for a ListView in case adapter has zero items in a Activity

开发者 https://www.devze.com 2023-01-25 16:08 出处:网络
How to use a layout as empty view for a listview when the adapter has zero elements? setEmptyView is not working with this code :

How to use a layout as empty view for a listview when the adapter has zero elements?

setEmptyView is not working with this code :

public class ZeroItemListActivity extends Activity {
    private ArrayList<String> listItems=new ArrayList<String>();
    private ListView mMyListView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInst开发者_如何学GoanceState);
        setContentView(R.layout.main);
        mMyListView=(ListView) findViewById(R.id.MyListView);
        mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
        LayoutInflater inflator=getLayoutInflater();
        View emptyView=inflator.inflate(R.layout.empty_list_item, null);
        //Empty view is set here
        mMyListView.setEmptyView(emptyView);
    }
    public void addItem(View v){
        listItems.add("list Item");
        mMyListView.invalidate();
    }
}

Layouts used : empty_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <Button android:id="@+id/Button01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Empty List,Click to add items"></Button>
</LinearLayout>

main.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:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ListView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/MyListView"></ListView>
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/AddItemsButton"
        android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>


The easiest way to get this done is to put the empty view in the main.xml layout:

    <?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:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:text="@string/hello" />
        <ListView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/MyListView">
        </ListView>
      <!-- empty view -->  
      <LinearLayout android:id="@+id/emptyView"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content">

            <Button android:id="@+id/Button01" 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:text="Empty List,Click to add items">
            </Button>
       </LinearLayout>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/AddItemsButton"
        android:text="Add Items" 
        android:onClick="addItem"></Button>
   </LinearLayout>

Then, in your code, set your empty view like this:

mMyListView.setEmptyView(findViewById(R.id.emptyView));

The reason your code isn't working is that the ListView and Empty view need to be in the same view heirarchy. And your call to inflate pass null as the parent so they are in different heirarchies.


The Android Dev tutorial for List Activity covers this topic: http://developer.android.com/reference/android/app/ListActivity.html

Basically have your class extend ListActivity. Set your list id to @id/android:listand the "empty view" id to @id/android:emptyin the same XML layout.

Hope this helps :)


When, inflating the view, you have to set the view parent to be part of the Layout hierarchy rather than pass null in some cases.

Replace

View emptyView = inflator.inflate(R.layout.empty_list_item, null);

with

View emptyView = inflator.inflate(R.layout.empty_list_item, (ViewGroup) mMyListView.getParent());

then you set the empty view as you did before with.

mMyListView.setEmptyView(findViewById(R.id.emptyView));
0

精彩评论

暂无评论...
验证码 换一张
取 消