列表视图仅显示数组列表的最后一项

发布于 2024-11-07 14:25:45 字数 2438 浏览 1 评论 0原文

我有一个问题。目前,我正在使用 Lazyloading 使用 arraylist 将图像和数据加载到我的列表视图中。但是它只显示数组列表的最后一项。

下面是 LazyLoading 适配器类:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<Hashtable<String,String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

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

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

public static class ViewHolder{
    public TextView title;
    public TextView author;
    public TextView format;
    public TextView call_number;
    public ImageView cover_image;

    public Button next;
    public Button previous;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if (convertView == null){
        vi = inflater.inflate(R.layout.search_result_display_list , null);
        holder=new ViewHolder();

        holder.next = (Button) vi.findViewById(R.id.next);
        holder.previous = (Button) vi.findViewById(R.id.previous);

        holder.title = (TextView) vi.findViewById(R.id.item_title);
        holder.author = (TextView) vi.findViewById(R.id.item_author);
        holder.format = (TextView) vi.findViewById(R.id.item_format);
        holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
        holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
        vi.setTag(holder);
    }
    else {
        holder=(ViewHolder)vi.getTag();
        vi = convertView;
    }

    for(int i = 0; i < getCount(); i++) {
        holder.title.setText(data.get(i).get("title"));
        holder.author.setText(data.get(i).get("author"));
        holder.format.setText(data.get(i).get("format"));
        holder.call_number.setText(data.get(i).get("callNumber"));
        holder.cover_image.setTag(data.get(i).get("coverImage"));
        imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
    }
    return vi;
}

我认为问题实际上可能出在我的 for 循环上。它被无限地产生。但我不知道我在输入这段代码时哪里出错了。请帮忙。

I have a problem. Currently i'm using Lazyloading to load images and data into my listview using arraylist. However it only display the last item of the arraylist.

Below is the LazyLoading adapter class:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<Hashtable<String,String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

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

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

public static class ViewHolder{
    public TextView title;
    public TextView author;
    public TextView format;
    public TextView call_number;
    public ImageView cover_image;

    public Button next;
    public Button previous;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if (convertView == null){
        vi = inflater.inflate(R.layout.search_result_display_list , null);
        holder=new ViewHolder();

        holder.next = (Button) vi.findViewById(R.id.next);
        holder.previous = (Button) vi.findViewById(R.id.previous);

        holder.title = (TextView) vi.findViewById(R.id.item_title);
        holder.author = (TextView) vi.findViewById(R.id.item_author);
        holder.format = (TextView) vi.findViewById(R.id.item_format);
        holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
        holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
        vi.setTag(holder);
    }
    else {
        holder=(ViewHolder)vi.getTag();
        vi = convertView;
    }

    for(int i = 0; i < getCount(); i++) {
        holder.title.setText(data.get(i).get("title"));
        holder.author.setText(data.get(i).get("author"));
        holder.format.setText(data.get(i).get("format"));
        holder.call_number.setText(data.get(i).get("callNumber"));
        holder.cover_image.setTag(data.get(i).get("coverImage"));
        imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
    }
    return vi;
}

I'm thinking that the problem might actually be with my for-loop. Where it is being generated infinitely. But I don't know where did I go wrong in typing this code. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2024-11-14 14:25:45

删除 for 循环并使用 position 代替 data.get(i) 中的 i

Remove the for loop and in place of i in data.get(i) use position

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文