Listview删除或隐藏一些项目而不会出错

时间:2015-05-21 13:19:50

标签: android listview

ListView删除或隐藏一些项目,下面是我的代码我想基本上按位置I或任何简单的方式隐藏列表视图中的一些项目。

我想隐藏或移除任何容易的内容,我不想在任何其他地方进行更改。请建议最简单的方法,没有任何错误:

我的代码在

下面
public class sAdapter extends BaseAdapter
{

    private List listData;
    private LayoutInflater mInflater;
    private List splitData;

    public int getCount()
    {
        return listData.size();
    }

    public Object getItem(int i)
    {
        return listData.get(i);
    }

    public long getItemId(int i)
    {
        return (long)i;
    }

    public View getView(int i, View view, ViewGroup viewgroup)
    {
        View view1;
        if(splitData.contains(listData.get(i)))
        {
            view1 = mInflater.inflate(0x7f030017, null);
        } else
        {
            view1 = mInflater.inflate(0x7f030018, null);
        }
        ((TextView)view1.findViewById(0x7f0900cc)).setText(((Map)listData.get(i)).get("itemTitle").toString());
        ((TextView)view1.findViewById(0x7f0900ce)).setText(((Map)listData.get(i)).get("value").toString());
        ((TextView)view1.findViewById(0x7f0900cd)).setText(((Map)listData.get(i)).get("unit").toString());
        ((ImageView)view1.findViewById(0x7f0900cb)).setImageResource(((Integer)((Map)listData.get(i)).get("imagehead")).intValue());
        return view1;
    }

    public boolean isEnabled(int i)
    {
        if(splitData.contains(listData.get(i)))
        {
            return false;
        } else
        {
            return super.isEnabled(i);
        }
    }

    public sAdapter(Context context, List list1, List list2)
    {
        super();
        mInflater = LayoutInflater.from(context);
        listData = list1;
        splitData = list2;
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个选项:

您可以在适配器上添加这样的方法:

public class sAdapter扩展了BaseAdapter   {

private List listData;
private LayoutInflater mInflater;
private List splitData;

public int getCount()
{
    return listData.size();
}

public Object getItem(int i)
{
    return listData.get(i);
}

public long getItemId(int i)
{
    return (long)i;
}

public View getView(int i, View view, ViewGroup viewgroup)
{
    View view1;
    if(splitData.contains(listData.get(i)))
    {
        view1 = mInflater.inflate(0x7f030017, null);
    } else
    {
        view1 = mInflater.inflate(0x7f030018, null);
    }
    ((TextView)view1.findViewById(0x7f0900cc)).setText(((Map)listData.get(i)).get("itemTitle").toString());
    ((TextView)view1.findViewById(0x7f0900ce)).setText(((Map)listData.get(i)).get("value").toString());
    ((TextView)view1.findViewById(0x7f0900cd)).setText(((Map)listData.get(i)).get("unit").toString());
    ((ImageView)view1.findViewById(0x7f0900cb)).setImageResource(((Integer)((Map)listData.get(i)).get("imagehead")).intValue());
    return view1;
}

public boolean isEnabled(int i)
{
    if(splitData.contains(listData.get(i)))
    {
        return false;
    } else
    {
        return super.isEnabled(i);
    }
}

public sAdapter(Context context, List list1, List list2)
{
    super();
    mInflater = LayoutInflater.from(context);
    listData = list1;
    splitData = list2;
}


public void removeItem(int position) {
    listData.remove(position);
    super.notifyDataSetChanged();
  }

}

然后在你想要触发删除项目的动作的监听器中执行此操作:

yourAdapter.removeItem(position);

"位置"是您要删除的项目的位置