In my app i have a listAdapter where iam loading the phonenumbers and their images from phonebook, as follows
public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) {
super(activity, textViewResourceId, listCont);
this.renderer = renderer;
this.listCont = listCont;
this.activity = activity;
this.profileType=profileType;
}
public class ViewHolder {
View rowView;
public TextView textEmailId;
public TextView textContNo;
public TextView text;
public QuickContactBadge photo;
public ViewHolder(View view)
{
rowView = view;
}
public TextView getTextView()
{
if(text ==null)
text = (TextView) rowView.findViewById(R.id.name);
return text;
}
public TextView getTextView1()
{
if(textContNo ==null)
textContNo = (TextView) rowView.findViewById(R.id.contactno);
return textContNo;
}
public TextView getTextView2()
{
if(textEmailId ==null)
textEmailId = (TextView) rowView.findViewById(R.id.emailId);
return textEmailId;
}
public QuickContactBadge getQuickContactBadge()
{
if(photo ==null)
photo = (QuickContactBadge) rowView.findViewById(R.id.quickContactBadge1);
return photo;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) (getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
view = inflater.inflate(renderer, null);
holder = new ViewHolder(view);
holder.text = (TextView) view.findViewById(R.id.name);
holder.textContNo = (TextView) view.findViewById(R.id.contactno);
holder.textEmailId = (TextView) view.findViewById(R.id.emailId);
holder.photo = (QuickContactBadge ) view.findViewById(R.id.quickContactBadge1);
view.setTag(holder);
} else {
holder =(ViewHolder) view.getTag();
}
Profile contact = listCont.get(position);
holder.text.setText(contact.getName());
QuickContactBadge photo = (QuickContactBadge ) view.findViewById(R.id.quickContactBadge1);
photo.setTag(contact.getMobileNo());
new LoadImage(photo).execute(contact.getMobileNo());
contact.getName();
contact.getId();
holder.textContNo.setText(contact.getNumber());
holder.textEmailId.setText(contact.getEmail());
return view;
}
and iam loading the images using the asynctask as below
class LoadImage extends AsyncTask<String, Void, Bitmap>{
private QuickContactBadge qcb;
public LoadImage(QuickContactBadge qcb) {
this.qcb= qcb;
}
@Override
protected Bitmap doInBackground( final String... params) {
activity.runOnUiThread(new Runnable() {
public void run() {
new QuickContactHelper(activity, qcb, (String) params[0]).addThumbnail();
}
});
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
}
iam facing the problem while scrolling the list view each time i scroll iam the images are changing for each contact, also scrolling is not smooth. i dnt know what am i doing wrong in code .Any help is appreciated.
It’s running very slow because you’re not executing anything on the background thread.
you’re just asking to re-execute back on the UI thread.
The image keeps changing because it shows the image that was before you scroll, and after that process completes it puts the new image. You should on the getView put some blank image or image placeholder so it will be blank, then the image.
Also try do some caching. Check the LruCache.
edit:
I’ve never worked with the QuickContactBadge before I’ll write how I would do code for ImageView and you can change the necessary parts.
inside the getView