删除List View Android中的重复行

时间:2016-07-20 07:01:08

标签: android listview android-adapter

我需要从ListView Android中删除重复行 下面是我的适配器代码。 我有一个HashMap,它包含Bean类数据的Date和ArrayList,需要设置如下图所示

屏幕1:当我将数据从15到20过滤时,我得到一个下面的列表,看起来很好15,从19到20,它的复制数据来自15

Screen 1

屏幕2:但是当我滚动查看20的数据时,它复制了一个臭名昭着的问题,即隐蔽视图看到了为Date 15映射的数据

Screen 2

屏幕3:此屏幕显示第20个日期的实际数据,仅为1行。

Screen 3 _ Real data for 20th Date

以下是我的适配器代码。如果我还需要一个持有人或任何其他建议,请建议。

public class AdapterReport extends BaseAdapter
{

    private ArrayList<LinearLayout> mLinearLayoutArry;
    private Context con;
    private ArrayList<String> keySet;
    private HashMap<String, ArrayList<FFBDetailsBean>> hashMapBeanList;

    public AdapterReport(Context con ,HashMap<String, ArrayList<FFBDetailsBean>> hashMapBeanList)
    {
        // TODO Auto-generated constructor stub
        this.con = con;
        this.hashMapBeanList = hashMapBeanList;
        keySet = new  ArrayList<String>();
        Iterator it = hashMapBeanList.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            System.out.println(pair.getKey() + " = " + pair.getValue());
            keySet.add(""+pair.getKey());

        }

        Collections.sort(keySet);


    }


    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return hashMapBeanList.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0)
    {
        // TODO Auto-generated method stub
        return 0;
    }



    public class ViewHolder
    {
        LinearLayout mLinearLayout ;
        TextView mDateTxt;


        Animation anim;

        ViewHolder(Context context)
        {
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2)
    {
        final ViewHolder holder;
        ViewGroup vg = null;
        LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ArrayList<FFBDetailsBean> ffbBeanListArry = hashMapBeanList.get(keySet.get(position));

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.report_between_days_content_layout, vg);
            holder = new ViewHolder(con);
            holder.mDateTxt = (TextView)convertView.findViewById(R.id.componentDate);
            holder.mLinearLayout = (LinearLayout)convertView.findViewById(R.id.contentListLinearLayout);

            for (int i = 0; i < ffbBeanListArry.size(); i++) {

                View view; 
                LayoutInflater inflater2 = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                view = inflater2.inflate(R.layout.report_between_days_content_inner_layout, null);

                LinearLayout innerLayoutLinear  = (LinearLayout)view.findViewById(R.id.innerLinearLayout);

                TextView receiptDateValue = (TextView)view.findViewById(R.id.receiptNoValue);
                receiptDateValue.setText(ffbBeanListArry.get(i).getmFFBReceiptNo());

                TextView farmerCodeValue = (TextView)view.findViewById(R.id.farmerCodeValue);
                farmerCodeValue.setText(ffbBeanListArry.get(i).getmFFBFarmerCode());


                TextView quantityValue = (TextView)view.findViewById(R.id.quantityValue);
                quantityValue.setText(ffbBeanListArry.get(i).getmFFBQuantity());

                holder.mLinearLayout.addView(innerLayoutLinear);

            }

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mDateTxt.setText(keySet.get(position));

        return convertView;
    }

2 个答案:

答案 0 :(得分:0)

编辑了我的代码,如下所示:

holder.mLinearLayout.removeAllViews();行为我工作

@Override
    public View getView(int position, View convertView, ViewGroup arg2)
    {
        final ViewHolder holder;
        ViewGroup vg = null;
        LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ArrayList<FFBDetailsBean> ffbBeanListArry = hashMapBeanList.get(keySet.get(position));

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.report_between_days_content_layout, vg);
            holder = new ViewHolder(con);
            holder.mDateTxt = (TextView)convertView.findViewById(R.id.componentDate);
            holder.mLinearLayout = (LinearLayout)convertView.findViewById(R.id.contentListLinearLayout);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mDateTxt.setText(keySet.get(position));
        holder.mLinearLayout.removeAllViews();
        for (int i = 0; i < ffbBeanListArry.size(); i++) {

            View view; 
            LayoutInflater inflater2 = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            view = inflater2.inflate(R.layout.report_between_days_content_inner_layout, null);

            LinearLayout innerLayoutLinear = (LinearLayout)view.findViewById(R.id.innerLinearLayout);

            TextView receiptDateValue = (TextView)view.findViewById(R.id.receiptNoValue);
            receiptDateValue.setText(ffbBeanListArry.get(i).getmFFBReceiptNo());

            TextView farmerCodeValue = (TextView)view.findViewById(R.id.farmerCodeValue);
            farmerCodeValue.setText(ffbBeanListArry.get(i).getmFFBFarmerCode());


            TextView quantityValue = (TextView)view.findViewById(R.id.quantityValue);
            quantityValue.setText(ffbBeanListArry.get(i).getmFFBQuantity());

            holder.mLinearLayout.addView(innerLayoutLinear);

        }




        return convertView;
    }

答案 1 :(得分:-1)

Add listView.invalidateViews(); and listView.notifyDatasetChanged()

可能会帮助你