1

I have a custom ArrayAdapter in android for populating my Custom ListView. However only the last item is displayed on my ListView and is iterated from the first item to the last(Last item in my array is displayed x times in listview where x is the number of items in the array). I have tried searching for answers but these address recycling of views which make the listview repeat the first few items. However mine repeats the last item from start to finish. I tried the suggestions there but still my code runs the same. The array contents are fine as the correct ones print in the log. Any help is appreciated.Here is my code

    public class VideoQueueAdapter extends BaseAdapter implements View.OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
VideoListModel vid_list_item=null;
int i=0;
private Context context;

/*************  CustomAdapter Constructor *****************/
public VideoQueueAdapter(Activity a, ArrayList d,Resources resLocal) {
    this.context = context;
    /********** Take passed val0ues **********/
    activity = a;
    data=d;
   res = resLocal;
    context = activity.getApplicationContext();
    /***********  Layout inflator to call external xml layout () **********************/
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

/******** What is the size of Passed Arraylist Size ************/
public int getCount() {

    if(data.size()<=0)
        return 1;
    return data.size();
}

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

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

/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{

    public TextView title;
    public TextView url;
    public TextView deadline;
    public TextView id;
    public ImageView thumbnail;

}

/*********** Depends upon data size called for each row , Create each ListView row ***********/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;

    if(vi==null){

        /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
        vi = inflater.inflate(R.layout.activity_video_item, null);

        /******** View Holder Object to contain tabitem.xml file elements ************/
        holder=new ViewHolder();

    }
    else{  holder=(ViewHolder)vi.getTag();}
    holder.thumbnail = (ImageView)vi.findViewById(R.id.vid_thumb);
    holder.title = (TextView)vi.findViewById(R.id.vid_title);
    holder.url = (TextView)vi.findViewById(R.id.vid_url);
    holder.deadline = (TextView)vi.findViewById(R.id.item_timeout);
    holder.id = (TextView)vi.findViewById(R.id.item_id);

    /************  Set holder with LayoutInflater ************/
    vi.setTag(holder);
    if(data.size()<=0)
    {
        holder.title.setText("No Data");

    }
    else
    {
        /***** Get each Model object from Arraylist ********/
        vid_list_item=null;
        vid_list_item = (VideoListModel) data.get(position);

        /************  Set Model values in Holder elements ***********/
        String title = "";

        String img = vid_list_item.getUrl();
        img = "http://img.youtube.com/vi/"+ img+ "/0.jpg";
        Picasso.with(context).load(img).resize(115, 115).into(holder.thumbnail);

        holder.title.setText(vid_list_item.getTitle());
        holder.url.setText(vid_list_item.getUrl());
        holder.deadline.setText(vid_list_item.getTimeout());
        holder.id.setText(vid_list_item.getId());
        /******** Set Item Click Listner for LayoutInflater for each row ***********/
        vi.setOnClickListener(new OnItemClickListener(position));
    }
    return vi;
}

@Override
public void onClick(View v) {
    Log.v("CustomAdapter", "=====Row button clicked");
}

/********* Called when Item click in ListView ************/
private class OnItemClickListener  implements View.OnClickListener {
    private int mPosition;

    OnItemClickListener(int position){
        mPosition = position;
    }

    @Override
    public void onClick(View arg0) {
       // CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
       // sct.onItemClick(mPosition);

    }
}



}
  • @Nam hoang Your suggested url made me rethink about my code and found out that the problem was with constructing the array. I placed the code checking contents of the array at the wrong place making me believe that it was right. Apparently my problem was constructing the temporary object outside the loop populating my arraylist. This [link](http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) showed my what was wrong. Thanks for those who answered – Miguel Ting May 11 '15 at 23:05

2 Answers2

0

I think the correct way of using viewholder is something like this

 if(vi==null){

    /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
    vi = inflater.inflate(R.layout.activity_video_item, null);

    /******** View Holder Object to contain tabitem.xml file elements ************/
    holder=new ViewHolder();
    holder.thumbnail = (ImageView)vi.findViewById(R.id.vid_thumb);
    holder.title = (TextView)vi.findViewById(R.id.vid_title);
    holder.url = (TextView)vi.findViewById(R.id.vid_url);
    holder.deadline = (TextView)vi.findViewById(R.id.item_timeout);
    holder.id = (TextView)vi.findViewById(R.id.item_id);
}
else{  holder=(ViewHolder)vi.getTag();}

try this , maybe it solves your problem

Soroush
  • 116
  • 7
  • I forgot to mention that your suggestion was the original code. Some post here about duplication suggested placing everything after the holder = new Viewholder(); in the if block outside like what is in the code. Didn't really chang enaything though – Miguel Ting May 11 '15 at 22:39
0

Along with the above answers, also make sure that you've impelemented

 @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

in your adapter. :)

LEE
  • 3,335
  • 8
  • 40
  • 70