Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

android - Correct use of setEmtpyView in AdapterView

I'm really having trouble using the setEmptyView method. I tried it to implement it in GridView and ListView, but both of them didnt work. Here a sample codeblock:

 networkGames = (GridView) baseLayer.findViewById(R.id.all_game_grid_network);
 networkGames.setBackgroundResource(R.drawable.game_border);
 networkGames.setSelector(R.drawable.game_active_border);
 networkGames.setOnItemClickListener(new NetworkGameListener());
 networkGames.setEmptyView(View.inflate(baseLayer, R.drawable.no_network_games, null));
 networkGames.setAdapter(new NetworkAdapter());

The network adapter contains no items:

private class NetworkAdapter extends BaseAdapter {

        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return 0;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return null;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return 0;
        }

        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }

    }

I also tried to call networkGames.setAdapter(null), but this doesnt work either. My emtpyView looks like this:

<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
    android:text="There are currently no network games available. Start a new one."
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">
    </TextView>
</LinearLayout>

I really don't know what I'm doing wrong. I also read various tutorials, but none of them metnioned any problems.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="300dip"
        android:id="@+id/list_view" />
    <TextView
        android: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 :

ListView listView = (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter( new ArrayAdapter( this, R.layout.selected_spinner_view, new ArrayList() ) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...