Android ListView添加了两次项

Android ListView添加了两次项,android,json,list,adapter,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 * 1

我正在使用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
         }
    }
此时,项目将添加两次。奇怪的是,当我检查列表中的项目时,它们都井然有序,没有重复。我已经花了将近2个小时在这个问题上,任何建议都会很好。

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

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

我模模糊糊地记得,在定制的
列表视图
/
适配器
组合中,曾经出现过类似的问题,但我记不清解决方案是什么。您是否尝试过将新元素添加到
阵列列表
,然后更新
适配器
,而不是直接将新元素添加到
适配器
?如下所示:

ArrayList<Explore> arrayList = new ArrayList<Explore>();
另外,在
Explore
类中添加
构造函数将非常有益:

public Explore (Bitmap thumbnail, String url)
{
    super();
    this.thumbnail = thumbnail;
    this.url = url;
}
然后,您可以使用两行简单的代码将新元素添加到
列表视图中:

arrayList.add(new Explore(thumbnail, href));
exploresAdapter.notifyDataSetChanged();
请尝试以下操作
(a)

c) 我在下面声明了
imageView\u ExploreAdapter\u缩略图

private LayoutInflater inflater;
ImageView imageView_ExploreAdapter_Thumbnail;

使用log并检查
add
方法调用了多少次?您的getItem()看起来完全错误。我相信您在开始上下滚动列表时会遇到这个问题。??correct@ρаσρѕρєK日志说,它被调用的次数尽可能多,列表的大小是正确的,但内容只是doubled@MDMalik不是真的不,甚至第一个内容已经翻了一番。滚动没有任何效果。我的坏,从来没有真正注意到这一点。但我担心这并没有解决问题我很糟糕,实际问题是全局性的。bitmapFromUrl()无法从url检索图像,请使用旧的。所以从技术上讲,列表显示的项目数量是正确的,只需复制图片即可。很抱歉给你添了这么多麻烦。
public Explore (Bitmap thumbnail, String url)
{
    super();
    this.thumbnail = thumbnail;
    this.url = url;
}
arrayList.add(new Explore(thumbnail, href));
exploresAdapter.notifyDataSetChanged();
public ExploreListAdapter(Context ctx, List<Explore> explores) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
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;
}
private LayoutInflater inflater;
ImageView imageView_ExploreAdapter_Thumbnail;