ListView数据重复

时间:2016-03-28 03:57:41

标签: java android listview adapter

来自解析的XML的数据被送入ArrayList itemList。然后将符合条件的结果添加到filteredList,而filterList又绑定到我的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;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这个我希望它会帮助你

 @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;
        }