Correct Use Of Setemtpyview In Adapterview
Solution 1:
You need to add this same view in the layout in which you have added the AdapterView.
AdapterView only changes its visibility based on the contents in the adapter.
EDITED : Following layout and code works fine :
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ListViewandroid:layout_width="fill_parent"android:layout_height="300dip"android:id="@+id/list_view" /><TextViewandroid:id="@+id/empty_list_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="List view is empty"android:visibility="gone" /></LinearLayout>
Code :
ListViewlistView= (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter( newArrayAdapter( this, R.layout.selected_spinner_view, newArrayList() ) );
Solution 2:
Karan's answer is spot-on. The AdapterView indeed displays the empty view by merely changing its own visibility vs the empty view's visibility. However, the empty view layout does not necessarily need to be in the same XML file as that of the AdapterView. I found it useful to separate the two because I use a single layout XML file (containing a GridView) for multiple activities, but I wanted to specify different emptyView layouts for each of them.
The key is to add the empty view to the activity with addContentView. Remember to do this after you have called setContentView specifying your main AdapterView.
My code (in AbstractBaseActivity.onCreate) :
ViewmainView= inflater.inflate(R.layout.imagegrid, null);
GridViewgridView= (GridView)mainView.findViewById(R.id.gridView);
ViewemptyView= inflater.inflate(getIdOfEmptyView(), null, false);
gridView.setEmptyView(emptyView);
// Any additional processing on mainView
setContentView(mainView);
addContentView(emptyView, newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
In each child activity I override getIdOfEmptyView as follows:
@Override
protectedintgetIdOfEmptyView()
{
return R.layout.emptyView_Favorites;
}
Solution 3:
You can define external XML layout files, inflate and set it as an empty view. Only thing to remember is to set the parent of the inflated view.
ViewemptyView= inflater.inflate(R.layout.id_of_your_layoout_file, parent, false);
gridView.setEmptyView(emptyView);
or
listView.setEmptyView(emptyView);
Solution 4:
PacificSky's answer got me almost to what I needed, but adding the empty view to the activity with addContentView
caused the empty view to remain in the activity's container even after removing the fragment containing the ListView from the activity. Additionally, the slide in menu for the activity displayed under or mixed in with the empty view.
The solution for me was to add the empty view to the container passed in as a parameter to the fragment's onCreateView
:
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
mFragmentContainer = container;
mListView = (ListView) mLayout.findViewById(R.id.list_view);
mEmptyListView = inflater.inflate(R.layout.empty_list, null, false);
container.addView(mEmptyListView);
mListView.setEmptyView(mEmptyListView);
mListAdapter = newMyListAdapter(getActivity(), mList);
mListView.setAdapter(mListAdapter);
And remove the empty view from the container in the fragment's onDestroyView
:
@OverridepublicvoidonDestroyView(){
super.onDestroyView();
mFragmentContainer.removeView(mEmptyListView);
}
Solution 5:
For my case, gridView.setEmptyView(emptyView) was called but it's not show the emptyview. Unless I called gridView.setVisibility(View.VISIBLE), so it appear normally;
Post a Comment for "Correct Use Of Setemtpyview In Adapterview"