Android ListView适配器如何检测空列表?
我的活动中有一个ListView,它从SimpleAdapter的扩展类获取值。 当我在其中添加或删除一些数据时,此功能正常工作 但我的问题是,当我的项目列表为空时,我想用“No item inhere”之类的消息替换listView 我试着这样做:Android ListView适配器如何检测空列表?,android,listview,adapter,android-arrayadapter,Android,Listview,Adapter,Android Arrayadapter,我的活动中有一个ListView,它从SimpleAdapter的扩展类获取值。 当我在其中添加或删除一些数据时,此功能正常工作 但我的问题是,当我的项目列表为空时,我想用“No item inhere”之类的消息替换listView 我试着这样做: class FavAdapter extends SimpleAdapter{ Activity context; ArrayList<HashMap<String,String>> data; St
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;
}
使用该xml:
创建一个默认情况下包含“未找到任何项目”条目的列表视图。添加第一项后,重新输入列表并删除默认项。使用将视图设置为显示适配器是否为空的方法。上述答案听起来不错,但出于记录原因,您的方法似乎没有被调用,可能是因为您的getCount()实现返回类似于data.size()的内容-表示0。这意味着ListView根本不会调用getView 相反,您可以这样做:
public int getCount() {
if (data.size()==0) {
return 1;
}
return data.size();
}
我希望这会有所帮助。您正在某些活动中调用此适配器。在这里,适配器被设置为任何listview。在将适配器设置为列表视图之前,可以检查arraylist的大小。如果大小为零,则您可以通过制作hashmap向arraylist添加
“No item inhere”
消息。我尝试在我的ListView上使用setEmptyView,但在活动启动时使用空数据时,以及在删除ListView中的某些内容后数据变为空时,都无法使用该消息=(说明如何处理ListView中的空视图更改列表中的内容时,还必须调用BaseAdapter.notifyDataSetChanged()。如果适配器变为空,则必须调用NotifyDataSetionValidated()而不是notifyDataSetChanged());当活动开始时我的数据为空,但在listview中删除该数据时,该功能不起作用。无论如何,谢谢。与上述问题相同,删除列表中的行时,列表不会显示“未找到项”条目。无论如何,谢谢:)删除时,只需检查长度是否为零。然后(重新)添加消息。该代码帮助我使以前的代码正常工作,谢谢:)但是当我也捕获OnClick时,我可能会遇到“空”单元格的问题。因此,我将继续研究如何使用“setEmptyView”解决方案:染料,事实上,当我看到它时,我投了更高的票,因为它比我的提案更好、更正确。我很高兴你也学会了。
<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>
public int getCount() {
if (data.size()==0) {
return 1;
}
return data.size();
}