Android ListView添加了两次项目

时间:2015-02-02 19:14:42

标签: android json list adapter

我正在使用JSON从网站解析图像和链接,并将它们添加到ListView:

    exploresAdapter = new ExploreListAdapter(context, new ArrayList<Explore>());
    listView_Explore.setAdapter(exploresAdapter);

    Document document = Jsoup.connect(Server.EXPLORE_LINK).timeout(10 * 1000).get();

    Elements divs = document.select("div[class=module-img] a[href]");

    for (Element div : divs) {
        try {
            href = div.attr("href");

            Elements a = document.select("a[href=" + href + "] img[src]");
                    src = a.attr("src");

            final Bitmap thumbnail = Global.bitmapFromUrl(src);

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Explore explore = new Explore();
                    explore.setUrl(href);
                    explore.setThumbnail(thumbnail);

                    exploresAdapter.add(explore);
                 }
            });
         } catch (Exception any) {
         //broken link or images, skip
         }
    }

这是我的适配器:

public class ExploreListAdapter extends BaseAdapter {
public interface Callback {
    public void onPageRequested(int page);
}

private LayoutInflater inflater;

private List<Explore> explores;

public ExploreListAdapter(Context ctx, List<Explore> explores) {
    inflater = LayoutInflater.from(ctx);
    this.explores = explores;
}

@Override
public int getCount() {
    return this.explores.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    Explore explore = this.explores.get(position);

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_explore, parent, false);

        holder.imageView_ExploreAdapter_Thumbnail = (ImageView) convertView.findViewById(R.id.imageView_ExploreAdapter_Thumbnail);

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

    holder.imageView_ExploreAdapter_Thumbnail.setImageBitmap(explore.getThumbnail());

    return convertView;
}

static class ViewHolder {
    ImageView imageView_ExploreAdapter_Thumbnail;
}

public void add(Explore explore) {
    this.explores.add(explore);
    notifyDataSetChanged();
}

public void clear() {
    this.explores.clear();
    notifyDataSetChanged();
}

public List<Explore> getList() {
    return this.explores;
}
}

探索对象:

public class Explore {

private Bitmap thumbnail;
private String url;

public Explore() {

}

public void setThumbnail(Bitmap thumbnail) {
    this.thumbnail = thumbnail;
}

public void setUrl(String url) {
    this.url = url;
}

public Bitmap getThumbnail() {
    return thumbnail;
}

public String getUrl() {
    return url;
}
}

此时,项目会添加两次。奇怪的是,当我检查列表中的项目时,它们都是完美的顺序,没有重复。我花了将近2个小时,任何建议都会很棒。

2 个答案:

答案 0 :(得分:1)

您的getItem()方法似乎很可疑 - 您只返回ID,而不是实际项目。它应该是这样的:

@Override
public Object getItem(int position)
{
    return explores.get(position);
}

修改

我模糊地回忆起在同一点上使用自定义ListView / Adapter组合的类似问题,但我无法准确记住解决方案的内容。您是否尝试将新元素添加到ArrayList,然后更新Adapter而不是直接将新元素添加到Adapter?如下所示:

ArrayList<Explore> arrayList = new ArrayList<Explore>();

然后当你想要添加一个新元素时......

Explore explore = new Explore();
explore.setUrl(href);
explore.setThumbnail(thumbnail);                
arrayList.add(explore);
exploresAdapter.notifyDataSetChanged();

此外,在您的Constructor课程中添加Explore也是有益的:

public Explore (Bitmap thumbnail, String url)
{
    super();
    this.thumbnail = thumbnail;
    this.url = url;
}

然后,您可以使用两个简单的行向ListView添加新元素:

arrayList.add(new Explore(thumbnail, href));
exploresAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

尝试以下
a)

public ExploreListAdapter(Context ctx, List<Explore> explores) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

b)

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    try {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_explore, null);
            imageView_ExploreAdapter_Thumbnail = (ImageView) vi.findViewById(R.id.imageView_ExploreAdapter_Thumbnail);

        } else {
            imageView_ExploreAdapter_Thumbnail = (ImageView) vi.findViewById(R.id.imageView_ExploreAdapter_Thumbnail);

        }
 imageView_ExploreAdapter_Thumbnail.setImageBitmap(explore.getThumbnail());



    return vi;
}

c)我已在下面声明imageView_ExploreAdapter_Thumbnail

private LayoutInflater inflater;
ImageView imageView_ExploreAdapter_Thumbnail;