-1

I manage a listView that works well. It should not have a lot of data, it is for comments, but it can have picture which get it slow.

Each time a change is done in the data shown in arrayList I do :

adap = new ReviewListAdapter(ctx, comments); // Comments is the modified arrayList
    lv_reviews.setAdapter(adap);

But since I manage picture it get really slow.

Is there a way to speed it up??? Using notifyDataSetChanged?

Juliatzin
  • 18,455
  • 40
  • 166
  • 325

4 Answers4

1

I would suggest you review your getView() method in your adapter and I would use Picasso to load images into an ImageView because there is a 100 ways you could screw up loading an image.

EDIT: Make sure to use the common ViewHolder pattern. Take a look at, Making ListView Scrolling Smooth

Community
  • 1
  • 1
ejohansson
  • 2,832
  • 1
  • 23
  • 30
  • the thing is I must insert my image into a textview? is it possible with picasso?'? – Juliatzin Dec 24 '13 at 01:57
  • why do you have to insert a picture into a text view? and no, I don't think so. – ejohansson Dec 24 '13 at 01:59
  • Well I use a static format for my listview : Icon, Name, Time, Comment ( Mixed Content, can contain more that one pic). Comment is a TextView I manage with SpannableStringBuilder. It works well. – Juliatzin Dec 24 '13 at 02:09
  • Don't use a TextView to store an Image. It's a **text** view, not an **image** view. Instead make good use of the `getItemViewType(int position)` method of your adapter – Martin Marconcini Dec 24 '13 at 02:26
1

In addition to what @ejohansson has already said, you have to use getItemViewType to differentiate between a layout that contains a TextView and one that contains an Image.

See this stack overflow question for more information and examples. The rest (ViewHolder, Picasso, etc.) are just optimizations, but you have to have a good architecture from the ground up.

Community
  • 1
  • 1
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
1

As mentioned above. First use the ViewHolder pattern, also use picasso to make image caching easy, if you need to use ImageSpannables you can use picasso like below.

ViewHolder {
    ImageView icon;
    TextView name;
    TextView time;
    TextView comment;
}

In your Adapter getView() do this

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = new View(); // use a layout inflator to inflate view
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();

    if (holder == null) {
        holder.name = convertView.findViewById(R.id.nameView);
        ... // other views
    }

    holder.name.setText("new name");
    new SetImageOnTextViewAsyncTask(context, holder.comment).execute("http://...");
}

Create a AsyncTask which takes the view you want to set the Image on and utilise Picasso

public SetImageOnTextViewAsyncTask extends AsyncTask<String, Void, Bitmap bmp> {
    private final TextView textview;
    private final Context context;

    public SetImageOnTextViewAsyncTask(Context context, TextView textview) {
        this.textView = textview;
        this.context = context;
    }

    @Override
    protected String doInBackground(String... urls) {
        return Picasso.with(context).load(urls[0]).get(); // Picasso will cache you image and only download it once
    }

     @Override
    protected void onPostExecute(ImageView result) {
        textView.setText( new ImageSpan( result ), BufferType.SPANNABLE);
    }
}

Finally instead of creating a new adapter all the time, just update the data structure of it and call notifyDataSetChanged. Have a method called updateData or something like that where you append items or change them, depending of what you need to do.

Tosa
  • 658
  • 5
  • 15
0

i think help for u.

Adapter class.

public class Singer_Adapter extends BaseAdapter { 

private Context context; 
private final String image[]; 
private final String name[]; 
private final String singer_id[]; 

SharedPreferences.Editor sh1; 
public ImageLoader imageLoader; 

public Singer_Adapter(Context context, String image[], String name[], 
        String singer_id[]) { 
    this.context = context; 
    this.image = image; 
    this.name = name; 
    this.singer_id = singer_id; 

    sh1 = PreferenceManager.getDefaultSharedPreferences(context).edit(); 
    imageLoader = ImageLoader.getInstance(); 
} 

@Override
public int getCount() { 
    return image.length; 
} 

@Override
public Object getItem(int position) { 
    return position; 
} 

@Override
public long getItemId(int position) { 
    return position; 
} 

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

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

    final ViewHolder holder; 
    View gridView = convertView; 

    if (convertView == null) { 

        gridView = new View(context); 

        // get layout from mobile.xml 
        gridView = inflater.inflate(R.layout.singers_view_row, null); 

        holder = new ViewHolder(); 

        holder.image1 = (ImageView) gridView.findViewById(R.id.list_iamge_singers); 
        holder.name1 = (TextView) gridView.findViewById(R.id.list_singer_name_singers); 
        holder.singer_id1 = (TextView)                       gridView.findViewById(R.id.list_singer_id_singers); 
        holder.album_list = (TextView)       gridView.findViewById(R.id.btn_albumlist_singers); 

        gridView.setTag(holder); 

    } else { 
        holder = (ViewHolder) gridView.getTag(); 
    } 

//  imageLoader.DisplayImage(image[position].toString(), holder.image1); 
    imageLoader.init(ImageLoaderConfiguration.createDefault(context)); 
    imageLoader.displayImage(image[position].toString(), holder.image1,      CommonUtilities.options); 
    holder.name1.setText(name[position].toString()); 
    holder.singer_id1.setText(singer_id[position].toString()); 

    holder.image1.setOnClickListener(new OnClickListener() { 

        @Override
        public void onClick(View arg0) { 

            /* 
             * holder.name1.setText(name[position].toString()); 
             * sh1.putString("singer_name", 
             * holder.name1.getText().toString()); 
             *  
             * sh1.putString("singer_image", image[position].toString()); 
             */

            holder.singer_id1.setText(singer_id[position].toString()); 
            sh1.putString("singer_id", holder.singer_id1.getText().toString()); 
            sh1.commit(); 

            Intent i = new Intent(context, Singers_Detail.class); 
            context.startActivity(i); 
        } 
    }); 

    holder.album_list.setOnClickListener(new OnClickListener() { 

        @Override
        public void onClick(View arg0) { 

            /* 
             * holder.name1.setText(name[position].toString()); 
             * sh1.putString("singer_name", 
             * holder.name1.getText().toString()); 
             *  
             * sh1.putString("singer_image", image[position].toString()); 
             */

            holder.singer_id1.setText(singer_id[position].toString()); 
            sh1.putString("singer_id", holder.singer_id1.getText().toString()); 
            sh1.commit(); 

            Intent i = new Intent(context, Singers_Detail.class); 
            context.startActivity(i); 
        } 
    }); 

    return gridView; 
} 

static class ViewHolder { 
    ImageView image1; 
    TextView singer_id1; 
    TextView name1; 
    TextView album_list; 
}
}
Dev 9
  • 263
  • 2
  • 5