0

When i used GridView and add custom layout to GridView i see this error and my app work very slow

  02-28 18:39:59.252: I/Choreographer(5586): Skipped 263 frames!  The application may be doing too much work on its main thread.

My code:

     public View getView(int position, View convertView, ViewGroup parent) {

        View vi1=convertView;
        if(convertView==null){

            vi1 = inflater.inflate(R.layout.my_layout, null);

            ImageView imv = (ImageView) vi1.findViewById(R.id.Image);
            ImageView imv2 = (ImageView) vi1.findViewById(R.id.imageView2);
            TextView tw1 = (TextView) vi1.findViewById(R.id.lName);
       //     TextView tw2 = (TextView) vi1.findViewById(R.id.County);
         //   TextView tw3 = (TextView) vi1.findViewById(R.id.user);
//          imageLoader.DisplayImage(null, imv);
    //        imageLoader.DisplayImage(null, imv2);
            tw1.setText("AZ");
        }


          return vi1;


    }
Musagil
  • 145
  • 1
  • 2
  • 12

2 Answers2

1

Your implementation for getView() is quite inefficient. Search for the "ViewHolder" or "RowWrapper" strategy for improvements. A good starting point is this resource on the Android dev site, but I'd definitely recommend going through Romain Guy's Google I/O 2010 presentation on this topic. There's a YouTube video of his session too, if you prefer that.

Your result should eventually look somewhat like this:

@Override public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.my_layout, parent, false);
        holder = new ViewHolder(); 
        holder.text = (TextView) convertView.findViewById(R.id.lName); 
        holder.img1 = (ImageView) convertView.findViewById(R.id.Image); 
        holder.img2  = (ImageView) convertView.findViewById(R.id.imageView2); 
        convertView.setTag(holder); 
    } else { 
        holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.text.setText("AZ"); 
    holder.img1.setImageBitmap(...); 
    holder.img2.setImageResource(...); 
    return convertView; 
}

static class ViewHolder { 
    TextView text; 
    ImageView img1, img2; 
}
MH.
  • 45,303
  • 10
  • 103
  • 116
0

There is problem in writing your getView Code.Your views are being created everytime for every row. Please find below the correct way to use getView method.You need to create a ViewHolder class.

 static class ViewHolder {
    ImageView img1;
    ImageView img2;
    TextView tvTitle;

}

      @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                ViewHolder holder;

                if (row == null) {
                    holder = new ViewHolder();
                    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                    row = inflater.inflate(R.layout.my_layout, parent, false);
                    holder.play = (Button) row.findViewById(R.id.btn_list_play);
                    holder.img1 = (ImageView) row.findViewById(R.id.img1_id);
                    holder.img2 = (ImageView) row .findViewById(R.id.img2_id);
                    holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle_id);
                    row.setTag(holder);
            }
        else
        {
                        holder = (ViewHolder ) row.getTag();

        }
                    holder.tvTitle.setText("AZ");

                return row;
            }
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
  • But there is the same problem :( – Musagil Feb 28 '13 at 18:28
  • According to RomainGuy the creater of Listview this method is the recommended method to inflate views in the Listview..There might be some other problem which might be causing the issue...If u could post some more code then it would be easier to diagnose.. – Karan_Rana Feb 28 '13 at 18:32