1

i have an arrayaadapter where i am retrieving the phone contacts numbers with images and displaying it in the list.

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

    View view = convertView; 

    if (view == null) { 
    LayoutInflater inflater = (LayoutInflater) (getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
    view = inflater.inflate(renderer, null); 
    } 


    TextView text = (TextView) view.findViewById(R.id.name); 
    TextView textContNo = (TextView) view.findViewById(R.id.contactno); 
    TextView textEmailId = (TextView) view.findViewById(R.id.emailId); 
    Profile contact = listCont.get(position); 
    text.setText(contact.getName());    

    QuickContactBadge photo = (QuickContactBadge ) view.findViewById(R.id.quickContactBadge1);  
    photo.setTag(contact.getMobileNo()); 
    new LoadImage(photo).execute(contact.getMobileNo()); 

and loading the images in backgroundthread using asyncTask

 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) { 

        } 
        }

i face two problems , the images are repeating and scrolling is not smooth iam trying to implemente viewholder in getview method but not sure how to use it or is there any other way to stop images from repeating. Any help is appreciated

teekib
  • 2,821
  • 10
  • 39
  • 75

2 Answers2

0

I think librairy android-query is made for you there in the documentation to help you: http://code.google.com/p/android-query/wiki/ImageLoading

letroll
  • 1,059
  • 1
  • 14
  • 36
0

To load image efficiently, Universal Image Loader is a good library to load images in background efficiently (Lazy Loading).

Some times what happen, when you are inflating any view first time, and scroll view, list has some limitation of repeating old views. The best solution to do is that you must use ViewHolder which holds your all views like

class ViewHolder
{
     View rowView;
     TextView textview = null;

     public ViewHolder(View view)
     {
          rowView = view;
     }

     public TextView getTextView()
     {
          if(textview ==null)
                textview = rowView.findViewById(R.id.text1);
          return textview;
     }
}  

and you can use it in your adapter like,

if(convertview ==null)
{
    convertview = inflater.inflate(renderer, null);
    ViewHolder holder = new ViewHolder(convertview);
    convertview.setTag(holder);
} 

ViewHolder tempHolder = (ViewHolder) convertview.getTag();

TextView textView = tempHolder.getTextView();

If you do this thing, your reference of textview and other views be hold in ViewHolder.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • so should i make all my view to be view class and later call it – teekib Dec 18 '12 at 15:35
  • 1
    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); view.setTag(holder); } else { holder =(ViewHolder) view.getTag(); } private static class ViewHolder { public TextView textEmailId; public TextView textContNo; public TextView text; } – teekib Dec 18 '12 at 15:36
  • Yes, this will let you create a good practice to work with adapters. – Chintan Rathod Dec 18 '12 at 15:47
  • iam doinf as below but getting nullpointerexcetion on line 27 http://pastebin.com/2S06YUQU – teekib Dec 18 '12 at 15:57
  • hi if u dnt mind can u please tell me how to integrate the Universal Image Loader library into my project – teekib Dec 19 '12 at 06:32
  • There is a tutorial how to use that library. http://www.intexsoft.com/blog/item/68-universal-image-loader-part-1.html – Chintan Rathod Dec 19 '12 at 07:05