Android ListView 添加了两次项目

标签 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 个小时,任何建议都会很棒。

最佳答案

您的 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();

此外,向您的 Explore 类添加一个 Constructor 将是有益的:

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

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

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

关于Android ListView 添加了两次项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285022/

相关文章:

Java - 确定用户输入\n和按html形式按回车之间的区别

java - 如何在 LinkedList 实现中的指定索引处添加节点

Android:如何在不同设备上绘制相同大小的ImageView?

javascript - 使用 ng-options 仅选择 JSON 对象的属性名称

android - 在 Android 中将 TextView 添加到 LinearLayout

java - jackson JSON : Unrecognized field, 未标记为可忽略

python - 如何在Python中根据属性对二维列表进行分组?

python - 删除以相同子字符串开头的第二个项目

android - 如何用ImageView制作边框?

android - 将类型转换器应用于 Dao Android Room