-3

I have a simple question. my way is the best way to implement a listView? my List have about 80 items so performance is important. I know using ViewHolder is good for performance but I did it in true way? my code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.event_list, null, false);
        holder = new ViewHolder();

        holder.title = (TextView) convertView.findViewById(R.id.textView2);
        holder.date = (TextView) convertView.findViewById(R.id.textView4);
        holder.location = (TextView) convertView.findViewById(R.id.textView3);
        holder.image = (NetworkImageView) convertView.findViewById(R.id.networkImageView);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.title.setText(eventList.get(position).getTitle());
    holder.date.setText(eventList.get(position).getDate());
    holder.location.setText(eventList.get(position).getLocation());

    if (imageLoader == null)
        imageLoader = ImageCacheManager.getInstance().getImageLoader();
    holder.image.setImageUrl(Constants.ImagesUrl + eventList.get(position).getIconFileName(), imageLoader);

    return convertView;
}

private class ViewHolder {
    public TextView title;
    public TextView date;
    public TextView location;
    public NetworkImageView image;
}
David
  • 2,129
  • 25
  • 34
  • 1
    view holder pattern in 99% of cases is useless, it can be only a root of the bugs (see how many questions about view holder pattern here) – pskink Dec 21 '15 at 11:23
  • 2
    Honestly, if we're talking about what's "best", then I would (and do) go for `RecyclerView` over `ListView` every time. [link](http://stackoverflow.com/questions/28525112/android-recyclerview-vs-listview-with-viewholder) – PPartisan Dec 21 '15 at 11:26
  • One extra thing, here no need to check inflater is null or not, also no need to create inflater object. – Nigam Patro Dec 21 '15 at 11:27
  • @NigamPatro `also no need to create inflater object` there is a need, it is used in `inflater.inflate(R.layout.event_list, null, false);` – pskink Dec 21 '15 at 11:31
  • @pskink See my posted answer, you can do like that way also, so no need to create inflater object. – Nigam Patro Dec 21 '15 at 11:34

3 Answers3

1

Since API 21, Android recyclerview implemented to improve the performance lists, with better preformance than ListView.

Show this: RecyclerView

Edit:

A suggestion, use Picasso Lib to load Async Images

Show this: Picasso

Santi
  • 130
  • 1
  • 6
  • what is pros of Picasso front of Volley? I mention volley's networkImageView is very simple to use. – David Dec 21 '15 at 12:28
  • mainly, Picasso is more simple (1 call static) and no need to extend another class. In performance, I do not think there's much difference – Santi Dec 21 '15 at 12:54
0

You can modify this line

convertView = inflater.inflate(R.layout.event_list, null, false);

to

convertView = LayoutInflater.from(context).inflate(R.layout.event_list, null, false);

and no you can remove this line

 if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

from getView().

Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
0

As @PPartisian mention a recyclerView can be considered as a better option.

But in your case (80 objects) a simple listview will do as most of the android phones has enough memory to load 80 objects.

There will be a trade of between a list view and a recyclerView or even other optimised methods.Each have there own pros and cons.

You can refer this.

Community
  • 1
  • 1
Abx
  • 2,852
  • 4
  • 30
  • 50