在适配器视图正确使用setEmtpyView的 [英] Correct use of setEmtpyView in AdapterView
问题描述
我使用的是setEmptyView方法真的有麻烦了。我试图在GridView和ListView控件来实现它,但他们都没有工作。下面的示例$ C $使用cblock:
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;
}
}
我也试过打电话给networkGames.setAdapter(空),但这并不工作,无论是。我emtpyView看起来是这样的:
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>
我真的不知道我在做什么错。我也看过各种教程,但他们没有metnioned任何问题。
I really don't know what I'm doing wrong. I also read various tutorials, but none of them metnioned any problems.
推荐答案
您需要添加同样的观点在其中所添加的适配器视图的布局。
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.
编辑:
继布局和code正常工作:
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:
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() ) );
这篇关于在适配器视图正确使用setEmtpyView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!