listview数据重复

dy2hfwbg  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(329)

解析后的xml中的数据被送入arraylist项列表。然后,满足条件的结果被添加到filteredlist,filteredlist又被绑定到要在我的listview中显示的适配器。但是,添加到filteredlist的数据被重复(两次),我不明白为什么。有人能帮我指出吗?我已经盯着它看了一段时间了,哈哈。
以下是mainactivity.java中的函数

private void FilterList() {

        ListView lv = (ListView) findViewById(R.id.info);    //locate listview

        for (int i = 0; i < itemList.size(); i++) //cycle arraylist
        {
            try {
                //convert startdate, endDate
                Date cDate = dateFormat.parse(currDate);     //get/convert selected date
                Date stDate = dateFormat.parse(itemList.get(i).startDate);
                Date enDate = dateFormat.parse(itemList.get(i).endDate);
                //compare, if active true then add
                if ((cDate.after(stDate)) && (cDate.before(enDate))){
                    //add itemList element to filterList
                    filteredList.add(itemList.get(i));
                    Log.d("DEBUG CHECKME", "added to filteredList: ");
                }
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
        }

        itemAdapter = new PostItemAdapter(mcontext, R.layout.postitem, filteredList);   //pass results into adapter
        lv.setAdapter(itemAdapter); //bind listview & adapter
            Log.d("DEBUG CHECKME", "lv.setAdapter");
    }

下面是我的适配器类:

public class PostItemAdapter extends ArrayAdapter<Item>
{
    private LayoutInflater inflater;
    private ArrayList<Item> datas;

    public PostItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects)
    {
        super(context, textViewResourceId, objects);

        inflater = ((Activity) context).getLayoutInflater();
        datas = objects;
    }

    //class to hold view object references
    static class ViewHolder
    {
        TextView itemTitleView;
        ImageView itemThumbView;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.postitem, null);

            viewHolder = new ViewHolder();
            viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
            viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
            //viewHolder.itemDateView = (TextView) convertView.findViewById(R.id.itemDateLabel);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (datas.get(position).itemThumbUrl == null)
        {
            viewHolder.itemThumbView.setImageResource(R.drawable.test);
        }

        viewHolder.itemTitleView.setText(datas.get(position).itemTitle);

        return convertView;
    }
}
epfja78i

epfja78i1#

试试这个,希望对你有帮助

@Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            View row = null;

            if (convertView == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.postitem, parent, false);
            } else {
                row = convertView;
            }
             filteredList datas= rowItems.get(position);
            try {
                TextView itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
               itemTitleView.setText(datas.itemTitle);

            } catch (Exception e) {

            }

            try {
            ImageView     itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
    itemThumbView.setImageResource(R.drawable.test);
            } catch (Exception e) {

            }

            });

            return row;
        }

相关问题