Search code examples
androidandroid-arrayadapterlayout-inflater

Error in ArrayAdapter - layout inflator thows error


Am getting the following warning in Eclipse:

"Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling"

The code which i had used is:

class myadapter extends ArrayAdapter<String>
{
    Context context;
    int[] images;
    String[] mytitle;
    String[] mydescp;
    myadapter(Context c, String[] tittle, int[] imgs, String[] desc)
    {
        super(c, R.layout.single_row, R.id.listView1, tittle);
        this.context=c;
        this.images=imgs;
        this.mytitle= tittle;
        this.mydescp=desc;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflator.inflate(R.layout.single_row, parent, false);
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView1);
        TextView myText = (TextView) row.findViewById(R.id.textView1);
        TextView mydesc = (TextView) row.findViewById(R.id.textView2);

        myImage.setImageResource(images[position]);
        myText.setText(mytitle[position]);
        mydesc.setText(mydescp[position]);

        return row;
    }
}

Am getting warning in the line : View row = inflator.inflate(R.layout.single_row, parent, false);

And it causes my android application to Force Close... What can i do it now?? Any Suggestions???


Solution

  • You need to recycle your views.What android as a system cares about is only the items that are visible.So you have to recycle the row items which are out of focus to be re-used for the newitems. Or else imagine the amount of caching involved.

    @Override
    public View getView(int position, View row, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(row==null){
         LayoutInflater inflator = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.single_row, parent, false);
      }
    
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView1);
        TextView myText = (TextView) row.findViewById(R.id.textView1);
        TextView mydesc = (TextView) row.findViewById(R.id.textView2);
    
        myImage.setImageResource(images[position]);
        myText.setText(mytitle[position]);
        mydesc.setText(mydescp[position]);
    
        return row;
    }