10

我想getView()为列表的每个项目调用,但我的问题是,如果与该行关联的数据为空,我们能否不显示该行。我只想在里面做这个getView ()

例如:

如果要在列表视图中显示姓名的人的警报为空。我不想在列表视图中显示该人的姓名。

4

2 回答 2

25

在返回视图之前,您可以在最后一行中将该行的可见性设置为“消失” - 这应该适合您。

编辑:如果内容不为空,请务必将可见性设置为可见 - 否则由于 ListView 重用视图,所有视图都将“消失”。

myView.setVisibility((myData == null) ? View.GONE : View.VISIBLE);
于 2012-06-17T11:12:52.713 回答
1

嗨伙计们,我尝试将可见性设置为不可见,但我仍然可以在列表视图中看到一个空行,所以我进行了一些更改,它根据我的需要工作,所以如果有人需要像我这样的类似结果,值得分享。

我使用列表 作为源我的要求是跳过几个应用程序所以我所做的就是从列表中删除该应用程序并通知数据集更改

public AppListAdapter(Context context, List<ApplicationInfo> appList) {
        this.context = context;
        this.appList = appList;
        inflater = LayoutInflater.from(context);
        localStorage = new LocalStorage(context);
        pm = context.getPackageManager();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
              convertView = inflater.inflate(R.layout.applist_item, parent, false);
            // Other lines of code
        if(appName.length()==0 || getItem(position).packageName.equals("package.name.to.skip")) {
              appList.remove(position);
              notifyDataSetChanged();
        }
    return convertView;
}

关注点

appList.remove(位置);
notifyDataSetChanged();

随时欢迎您的建议和更正。
阿兹马特罕宰

于 2016-07-30T21:43:22.857 回答