Android ListView Adapter 如何检测空列表?
我的 Activity 中有一个 ListView,它从 SimpleAdapter 的扩展类获取值。 当我添加或删除其中的一些数据时,这个可以正常工作。
但我的问题是,当我的项目列表为空时,我想用“此处没有项目”或类似的消息替换 listView。
我尝试这样做:
class FavAdapter extends SimpleAdapter{
Activity context;
ArrayList<HashMap<String,String>> data;
String[] items;
int[] champs;
int layout;
FavAdapter(Activity _context, ArrayList<HashMap<String,String>> _data, int _layout, String[] _items, int[] _champs){
super(_context, _data, _layout, _items, _champs );
this.data = _data;
this.context = _context;
this.items = _items;
this.layout = _layout;
this.champs = _champs;
}
public View getView(int pos, View convertView, ViewGroup parent){
//Recycling
View row = convertView;
//Else get from XML
if(row == null){
LayoutInflater infla = context.getLayoutInflater();
row = infla.inflate(layout, null);
}
//If there are data
if(data.size() > 0){
/**** Here I am correctly constructing row *****/
...
}
//Else
else{
TextView txt = new TextView(ActivityFavoris.this);
txt.setText("Not data inhere");
LinearLayout ll = (LinearLayout) row.findViewById(R.id.result_lyt_principal);
ll.addView(txt);
}
return row;
}
问题是:我的列表的大小从来没有为 0。事实上,当数据为空时,不会调用适配器。
有人可以向我解释如何用另一种方式做到这一点吗? 提前致谢。
编辑: 使用你的答案我成功了。我的解决方案是: setContentView 必须与 findViewById(android.R.id.empty) 一起使用,但是如果像我一样,您的活动中有两个不同的 listView,它们需要不同的空视图,您将必须在不同的布局中找到它。这样,当listview为空时,android将自动调用正确的视图,并且当data.size为0时,适配器中无需执行任何操作。
示例:在Activity中:
public void onCreate(Bundle b){
super.onCreate(b);
lv1 = (ListView) findViewById(R.id.listviewone);
lv2 = (ListView) findViewById(R.id.listviewtwo);
//****set listview adater***//
...
//set empty view
lv1.setEmptyView(findViewById(R.id.layoutone).findViewById(android.R.id.empty));
lv2.setEmptyView(findViewById(R.id.layouttwo).findViewById(android.R.id.empty));
}
使用该 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layoutone"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listviewone"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
<LinearLayout
android:id="@+id/layouttwo"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listviewtwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
I have a ListView in my Activity which takes values from an extension class of SimpleAdapter.
This one is correctly working when I add or delete some data in it.
But my problem is, when my item list is empty, I would like to replace the listView by a message like "No item inhere" or something like that.
I tried to do it, this way:
class FavAdapter extends SimpleAdapter{
Activity context;
ArrayList<HashMap<String,String>> data;
String[] items;
int[] champs;
int layout;
FavAdapter(Activity _context, ArrayList<HashMap<String,String>> _data, int _layout, String[] _items, int[] _champs){
super(_context, _data, _layout, _items, _champs );
this.data = _data;
this.context = _context;
this.items = _items;
this.layout = _layout;
this.champs = _champs;
}
public View getView(int pos, View convertView, ViewGroup parent){
//Recycling
View row = convertView;
//Else get from XML
if(row == null){
LayoutInflater infla = context.getLayoutInflater();
row = infla.inflate(layout, null);
}
//If there are data
if(data.size() > 0){
/**** Here I am correctly constructing row *****/
...
}
//Else
else{
TextView txt = new TextView(ActivityFavoris.this);
txt.setText("Not data inhere");
LinearLayout ll = (LinearLayout) row.findViewById(R.id.result_lyt_principal);
ll.addView(txt);
}
return row;
}
Problem is: my list has never a size of 0. in fact when the data is empty the adapter is not called.
Can someone explain me how I can do it by another way?
Thanks in advance.
Edit : Using your answers I succeed on that. My solution is : setContentView has to be used with findViewById(android.R.id.empty), but if like me you have two different listView in your activity, which need different empty views, you will have to find it inside different layout. This way android will automatically call the right view when the listview is empty, and nothing have to be done in the adapter when data.size is 0.
example: in Activity :
public void onCreate(Bundle b){
super.onCreate(b);
lv1 = (ListView) findViewById(R.id.listviewone);
lv2 = (ListView) findViewById(R.id.listviewtwo);
//****set listview adater***//
...
//set empty view
lv1.setEmptyView(findViewById(R.id.layoutone).findViewById(android.R.id.empty));
lv2.setEmptyView(findViewById(R.id.layouttwo).findViewById(android.R.id.empty));
}
with that xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layoutone"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listviewone"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
<LinearLayout
android:id="@+id/layouttwo"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listviewtwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用方法 setEmptyView 其中设置视图以显示适配器是否为空。
Use the method setEmptyView which sets the view to show if the adapter is empty.
上面的答案听起来不错,但根据记录,您的方法似乎没有被调用的原因可能是因为您的 getCount() 实现返回类似 data.size() 的内容 - 意思是 0。这意味着 ListView 永远不会调用 getView根本不。
相反,你可以做这样的事情:
我希望这有帮助。
The above answer sounds fine, but for the record the reason your method doesn't appear to be called is probably because your implementation of getCount() returns something like data.size() - meaning 0. This means the ListView will never call getView at all.
Instead you could do something like this:
I hope that's helpful.
您在某些活动中调用此适配器。适配器设置为任何列表视图。在将适配器设置为列表视图之前,您可以检查 arraylist 的大小。如果大小为零,那么您可以通过创建哈希图将
“No item inhere”
消息添加到数组列表You are calling this adapter in some activity. there the adapter is set to any listview. Before setting the adapter to the list view you can check what is the size of arraylist. if the size is zero then you can add
"No item inhere"
message to the arraylist by make a hashmap默认情况下创建一个带有“未找到项目”条目的列表视图。添加第一个项目后,重新初始化列表并删除默认条目。
Create a listview with the 'no items found' entry by default. When the first item is added, reinit the list and remove the default entry.