-1

I'm working on an android app from a book. I'm getting errors : "crimeHolder cannot be resolved" and I can't understand why..

 public View getView(int poisition, View convertView , ViewGroup parent)
        {
            //If we weren't given a view, inflate one
            if (convertView == null)
            {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
                ViewHolder crimeHolder = new ViewHolder();
                crimeHolder.titleTextView = (TextView)convertView.findViewById(R.id.listItemTitleTextView);
                crimeHolder.dateTextView = (TextView)convertView.findViewById(R.id.listItemDateTextView);
                crimeHolder.solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crimeListItemSolvedCheckBox);

                // store the holder with the view
                convertView.setTag(crimeHolder);
            }

            //Configure the view for this Crime
            Crime crime = getItem(poisition);

            if (crime!=null)
            {

These 3 show the errors :

                crimeHolder.titleTextView.setText(crime.getTitle());
                crimeHolder.dateTextView.setText(crime.getDate().toString());   
                crimeHolder.solvedCheckBox.setChecked(crime.isSolved());
            }
            return convertView;

        }
    }

    static class ViewHolder
    {
        TextView titleTextView;
        TextView dateTextView;
        CheckBox solvedCheckBox;
    }
BVtp
  • 2,308
  • 2
  • 29
  • 68

2 Answers2

1

Try like this...

public View getView(int poisition, View convertView , ViewGroup parent)
                {
    ViewHolder crimeHolder = null;
                    //If we weren't given a view, inflate one
                    if (convertView == null)
                    {
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
                        crimeHolder = new ViewHolder();
                        crimeHolder.titleTextView = (TextView)convertView.findViewById(R.id.listItemTitleTextView);
                        crimeHolder.dateTextView = (TextView)convertView.findViewById(R.id.listItemDateTextView);
                        crimeHolder.solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crimeListItemSolvedCheckBox);

                        // store the holder with the view
                        convertView.setTag(crimeHolder);
                    }else{
                        crimeHolder=(ViewHolder)convertView.getTag();
                    }
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
  • Works perfectly. Thanks! Can you please explain me the setTag and getTag ? In the book , and other tutorials explaining ViewHolder pattern I've read, they didn't mention anything about getTag , so I'd be happy to learn your implementation. – BVtp Jun 22 '15 at 07:00
  • @BVtp view holder pattern has almost no performance gain in your app, don't use it, it really makes more hassle than real speed benefit – pskink Jun 22 '15 at 07:14
  • @BVtp Developers android says: Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure. Also I've found the information about `setTag() getTag()` here http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – Max Zavernutiy Jun 22 '15 at 07:35
  • Check the link http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder – Chandrakanth Jun 22 '15 at 07:38
  • Check the link http://daniel-codes.blogspot.com/2013/11/is-findviewbyid-slow.html – pskink Jun 22 '15 at 07:42
0

You declare crimeHolder in if block, so out of this block it's not available. Please try below code

public View getView(int poisition, View convertView , ViewGroup parent)
        {
            ViewHolder crimeHolder = null;
            //If we weren't given a view, inflate one
            if (convertView == null)
            {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
                crimeHolder = new ViewHolder();
                crimeHolder.titleTextView = (TextView)convertView.findViewById(R.id.listItemTitleTextView);
                crimeHolder.dateTextView = (TextView)convertView.findViewById(R.id.listItemDateTextView);
                crimeHolder.solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crimeListItemSolvedCheckBox);

                // store the holder with the view
                convertView.setTag(crimeHolder);
            }

            //Configure the view for this Crime
            Crime crime = getItem(poisition);

            if (crime!=null)
            { crimeHolder.titleTextView.setText(crime.getTitle());
            crimeHolder.dateTextView.setText(crime.getDate().toString());   
            crimeHolder.solvedCheckBox.setChecked(crime.isSolved());
        }
        return convertView;

    }
}

static class ViewHolder
{
    TextView titleTextView;
    TextView dateTextView;
    CheckBox solvedCheckBox;
}
Phuc Tran
  • 7,555
  • 1
  • 39
  • 27
  • When I do that I get an error "The local variable crimeHolder may not have been initialized" , but initializing crimeHolder as null in the beginning solves it. Thank you! – BVtp Jun 22 '15 at 06:58
  • Updated my answer. Accept & vote it if it works for you :) – Phuc Tran Jun 22 '15 at 07:02
  • I've already accepted the first answer. I can't accept both right? and It says I can't vote yet :/ I'm sorry.. btw, why is my question -2 ? – BVtp Jun 22 '15 at 07:04
  • That's fine! No problem. – Phuc Tran Jun 22 '15 at 07:15