如何从适配器的 getItemView() 方法内的列表视图中删除项目?

Posted

技术标签:

【中文标题】如何从适配器的 getItemView() 方法内的列表视图中删除项目?【英文标题】:How to remove an item from a listview inside the getItemView() method of the adapter? 【发布时间】:2014-09-25 21:44:01 【问题描述】:

我想检查一个条件,如果是,我想在那个方法中隐藏项目。我不认为我可以使用项目的位置,因为我在方法中。我该怎么做呢?谢谢。

代码:

public View getItemView(ParseObject object, View v, ViewGroup parent)

        if(v == null)
            v = View.inflate(ctx, R.layout.adapter_item, null);
        

        super.getItemView(object, v, parent);
        ParseUser current = ParseUser.getCurrentUser();
        Order order = (Order) object;

        TextView t1 = (TextView)v.findViewById(R.id.textView1);
        TextView t2 = (TextView)v.findViewById(R.id.textView2);

        if(current.getUsername().equals(order.getString("userA")))
            if(order.getBoolean("newA") == true)
                t1.setTypeface(null, Typeface.BOLD);
                t2.setTypeface(null, Typeface.BOLD);
            

            if(order.getBoolean("hideA") == true)
                //hide item
            
        

        

        return v;



    

【问题讨论】:

您想如何隐藏该项目?如果您正在谈论隐藏该行,那么您可以将可见性设置为 gone 那不会占用空间吗?我将如何隐藏整行? 没有。 v.setVisibility(View.GONE) 或者,您可以检查这些条件设置您的适配器然后从列表中删除/不添加它们 可能重复 - ***.com/questions/2638160/… 【参考方案1】:
    t1.setText("");
    t2.setText("");

不是最好的解决方案,但这会奏效。

【讨论】:

【参考方案2】:

也可以这样工作:

yourListItem.remove(itemA);
yourListView.getAdapter().notifyDataSetChange();

【讨论】:

以上是关于如何从适配器的 getItemView() 方法内的列表视图中删除项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何从片段内的列表视图打开链接网址?

删除房间内数据库

如何从 MainActivity 中的 Fragment 访问适配器?我正在使用带有 viewpager 的标签,所以没有交易等方法

从列表视图的自定义适配器内的意图服务接收结果

从片段更新ListView适配器内的TextView

如何在适配器内进行异步适配器调用