0

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god. It works! Been staring at this error for hours and I didn't even think of using position! Haha. Thanks!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.