2

私のアプリには、次のように、電話番号とその画像を電話帳からロードする listAdapter があります。

 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;
    }

以下のように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) { 

        } 

スクロールするたびにリストビューをスクロールしているときに問題に直面しています。連絡先ごとに画像が変化しており、スクロールもスムーズではありません。コードで何が間違っているのかわかりません。どんな助けでも大歓迎です。

4

3 に答える 3

1

バックグラウンド スレッドで何も実行していないため、実行速度が非常に遅いです。

    activity.runOnUiThread(new Runnable() { 
    public void run() { 
    new QuickContactHelper(activity, qcb, (String) params[0]).addThumbnail(); 
    } 
    }); 

UI スレッドで再実行するよう求めているだけです。

スクロールする前の画像が表示され、そのプロセスが完了すると新しい画像が配置されるため、画像は変化し続けます。getView に空白の画像または画像のプレースホルダーを配置して、空白にしてから画像にする必要があります。

また、いくつかのキャッシュを試してください。LruCache を確認します。

編集:

ImageView のコードを作成する方法を書く前に、QuickContactBadge を使用したことがなく、必要な部分を変更できます。getView内

public View getView(int position, View convertView, ViewGroup parent) {
   // all that processing to get the data then...
   img.setImageResource(R.id.blank_user); // put something image placeholder (blank)
   new LoadImage(img).execute(url); // download

}
于 2012-12-20T11:05:20.457 に答える
0

null値をImageViewに設定してみてください。

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

   img.setImageResource(null); 
   new LoadImage(img).execute(url); 

}

わたしにはできる..

于 2013-03-30T12:46:56.570 に答える
0

このブログをご覧になることをお勧めします: https://sriramramani.wordpress.com/2012/07/22/recycling-listview-rows/

コアコードは次のとおりです。

theListView.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
    // Cast the view to the type of the view we inflated.
    MyCustomListRow row = (MyCustomListRow) view;

    // Get the view that holds the image and nullify the drawable.
    // GC will take care of cleaning up the memory used.
    row.getThatImageDrawableView().setImageDrawable(null);
}});
于 2014-02-02T12:18:44.740 に答える