gpt4 book ai didi

Android ListView 添加了两次项目

转载 作者:行者123 更新时间:2023-11-30 02:22:26 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com