开发者

ListView android:empty in code

When creating a custom ListView in XML, you can add the android:id/empty tag to the ID to make that view show when the ListView is empty; as mentioned in this Android documentation. I'm just wondering, does anyone know how to do the equivalent dynamically in code?

UPDATE This is what I have:

public class CustomList extends ListView {
     public CustomList(Context context) {
         // some initialization
开发者_运维知识库         TextView text = new TextView(context);
         // some parameters like text size, color, and font are set here
         setEmptyView(text);
     }
}

The setEmptyView seems ineffective, in fact is just causes everything to be gone when the ListView goes empty.


Very simple. Use FrameLayout.

Frame layouts are one of the simplest and most efficient types of layouts used by Android developers to organize view controls. They are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The frame layout is often used as a container layout, as it generally only has a single child view (often another layout, used to organize more than one view).

For example :

<FrameLayout android:id="@+id/list_and_empty_message_container"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:orientation="vertical">
        <!-- List view -->
        <ListView android:id="@+id/main_list_view"
            android:background="@android:color/transparent"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:cacheColorHint="#00000000" android:layout_gravity="top"
            android:divider="#D7C3F8" android:dividerHeight="2dip" />
        <TextView android:text="No info Found." android:id="@+id/list_empty_txt"
            android:layout_height="wrap_content" android:textSize="16.1sp"
            android:textStyle="bold" android:typeface="monospace"
            android:layout_marginLeft="5dip" android:layout_gravity="center_vertical"
            android:gravity="center" android:textColor="#000000"
            android:layout_width="fill_parent" android:visibility="gone" />
    </FrameLayout>

And count your data adapter. If that is empty set textview as visible, else set visibility to gone.

Put below code in your onCreate(),

TextView emptyText =  (TextView) findViewById(R.id.list_empty_txt);;
        if(Your_data_array.size() <= 0)
            emptyText.setVisibility(View.VISIBLE);
        else
            emptyText.setVisibility(View.GONE);

It will work.

* ___________Updated__________*

Take a look in ListActivity class, there is a method that sets empty message if your data source is empty.

public void onContentChanged() {  
    super.onContentChanged();  
    View emptyView = findViewById(com.android.internal.R.id.empty);  
    mList = (ListView)findViewById(com.android.internal.R.id.list);  
    if (mList == null) {  
        throw new RuntimeException(  
                "Your content must have a ListView whose id attribute is " +  
                "'android.R.id.list'");  
    }  
       //Take a look here
    if (emptyView != null) {  
        mList.setEmptyView(emptyView);  
    }  
    mList.setOnItemClickListener(mOnClickListener);  
    if (mFinishedStart) {  
        setListAdapter(mAdapter);  
    }  
    mHandler.post(mRequestFocus);  
    mFinishedStart = true;  
} 

I will help you to solve your problem.

Happy coding :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜