0

Below is the code for my custom list adapter with for listview. The adapter works perfectly if I comment out the "not null" token in my code,that is without my view holder. Can someone help me with the problem I have here?

public View getView(int i, View view, ViewGroup viewGroup) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.eachlist, viewGroup, false);

        if (this.subject==null && this.percentage==null && this.happen==null && this.missed==null) {

            Log.d("null cha","error");
            subject = (TextView) row.findViewById(R.id.subjectname);
            happen=(TextView)row.findViewById(R.id.attended);
            missed=(TextView)row.findViewById(R.id.missed);
            percentage=(TextView)row.findViewById(R.id.Attendance);

            new viewholder(subject,happen,missed,percentage);
        }
        else {

            subject=viewholder.subject;
            happen=viewholder.happen;
            missed=viewholder.missed;
            percentage=viewholder.percentage;

        }

        subject.setText(list.get(i).subject);
        happen.setText(String.valueOf(list.get(i).happened));
        missed.setText(String.valueOf(list.get(i).missed));
        percentage.setText(String.valueOf(list.get(i).percentage));

        return row;
    }
    static class viewholder{
        static TextView subject;
        static TextView happen;

        static TextView missed;
        static TextView percentage;

        viewholder(TextView subject,TextView happen,TextView missed,TextView percentage){
            this.subject=subject;
            this.happen=happen;
            this.missed=missed;
            this.percentage=percentage;

        }


    }
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
  • you are doing it wrong. Check this tutorial http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html – Murtaza Khursheed Hussain Dec 08 '14 at 13:27
  • Are you sure you're doing it correctly? The whole point of "view holder" is so that you don't inflate the UI and inspect it if one is already cached for you. You do this by setting the tag on your view and then checking whether or not this tag represents an instance of your ViewHolder class. See here for a detailed example http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html and make sure you understand the bits with .getTag() and .setTag(). – kha Dec 08 '14 at 13:29

1 Answers1

0

You should check if the view provided as parameter by the system (aka convertView) is null, and only in that case inflate your layout.

if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.eachlist, viewGroup, false);
        subject = (TextView) view.findViewById(R.id.subjectname);
        happen=(TextView)view.findViewById(R.id.attended);
        missed=(TextView)view.findViewById(R.id.missed);
        percentage=(TextView)view.findViewById(R.id.Attendance);
        ViewHolder v new ViewHolder(subject,happen,missed,percentage);
        view.setTag(v);
    }  else {
        ViewHolder v = (ViewHolder) view.getTag();
        subject=v.subject;
        happen=v.happen;
        missed=v.missed;
        percentage=v.percentage;

    }

   ..
  return view;
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thank you. I was under the impression that the findViewByID would have higher algorithm complexity, but the idea of the cost of the inflation itself never occured to me. I just wanted to work out my own way for the code, ended up with a terrible question. Thank you folks – Shakar Bhattarai Dec 08 '14 at 13:34