1

ツイアーフィーダーを使用していますが、viewHolderパターンを使用して画像を管理する方法がわかりません

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

    View v = convertView;
    if (v == null) {               
         inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
         v = inflater.inflate(R.layout.activity_result, null);
         holder = new ViewHolder();

         holder.name = (TextView) v.findViewById(R.id.name);
         holder.tweet = (TextView) v.findViewById(R.id.tweet);
         holder.avatar = (ImageView) v.findViewById(R.id.avatar);
         Log.v("TAG", "ACA SI LLEGE holder cargado");
         holder.name.setText(mitems.get(position).getFromUser());
         holder.tweet.setText(mitems.get(position).getText());


         task = new BackgroundAsyncTask(this, position);
         task.execute();


         v.setTag(holder);
    } else {              
     holder = (ViewHolder) convertView.getTag();          
     }                             
   return v; 

}



public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void>{

    private ResultsAdapter resultAdapter;
    private int position;
    private MyApplication myapp;
    private Bitmap bitmap;

    public BackgroundAsyncTask(ResultsAdapter adptr, int position){
        this.resultAdapter = adptr;
        this.position = position;
    }

    @Override
    protected Void doInBackground(Void... params) {

          try {
                for(int i = 0; i < mitems.size(); i++){               
                bitmap = BitmapFactory.decodeStream((InputStream)
                  new URL(mitems.get(i).getProfileImageUrl()).getContent()); 
          }
          } catch (MalformedURLException e) {   
              e.printStackTrace(); 
          } catch (IOException e) {   
              e.printStackTrace(); 
          } 

        return null;
    }

    @Override  
    protected void onProgressUpdate(Integer... values) {     
            myapp.setProgreso(values[0]);
    }
    @Override
    protected void onPostExecute(Void result){       
        holder.avatar.setImageBitmap(bitmap);
    }
4

1 に答える 1

0

解決:

public class ResultsAdapter extends BaseAdapter{

private Context mContext;
private LayoutInflater inflater;
private List<Result> mitems;
private List<ImageView> allImageViews = new ArrayList<ImageView>();
private ViewHolder holder;
private BackgroundAsyncTask task;

public ResultsAdapter(Context c, List<Result> mitems){
    mContext = c;
    inflater = LayoutInflater.from(mContext);
    this.mitems = mitems;
} 

@Override
public int getCount() {
    return mitems.size();
}

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

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

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

    View v = convertView;
    if (v == null) {               
         inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
         v = inflater.inflate(R.layout.activity_result, null);
         holder = new ViewHolder();          
         holder.name = (TextView) v.findViewById(R.id.name);
         holder.tweet = (TextView) v.findViewById(R.id.tweet);
         holder.avatar = (ImageView) v.findViewById(R.id.avatar);
         holder.name.setVisibility(holder.name.INVISIBLE);
         holder.tweet.setVisibility(holder.tweet.INVISIBLE);
         holder.name.setText(mitems.get(position).getFromUser());
         holder.tweet.setText(mitems.get(position).getText());

         allImageViews.add(holder.avatar);

         task = new BackgroundAsyncTask();
         task.execute();
         holder.name.setVisibility(holder.name.VISIBLE);
         holder.tweet.setVisibility(holder.tweet.VISIBLE);

         v.setTag(holder);
    } else {              
     holder = (ViewHolder) convertView.getTag();          
     }                             
   return v;    
}
public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void>{

    private MyApplication myapp;
    private List<Bitmap> bitmaps;
    private ProgressDialog dialog;

    public BackgroundAsyncTask(){
        bitmaps = new ArrayList<Bitmap>();
        dialog = new ProgressDialog(mContext);
        dialog.setTitle("Twitter Searching App");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Searching...");
        dialog.setCancelable(true);
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",   
                        new DialogInterface.OnClickListener() {   
                          public void onClick(DialogInterface dialog, int which) {   
                            dialog.cancel();    
                          }   
                        }   
                      );
    }

    @Override
    protected Void doInBackground(Void... params) {
         for(int i =0; i < allImageViews.size(); i++){                             
              try {               
                    bitmaps.add(BitmapFactory.decodeStream((InputStream)
                      new URL(mitems.get(i).getProfileImageUrl()).getContent())); 
              } catch (MalformedURLException e) {   
                  e.printStackTrace(); 
              } catch (IOException e) {   
                  e.printStackTrace(); 
              }                      
         }

        return null;
    }
    @Override
    protected void onPreExecute() {                 
        dialog.show();     
    }

    @Override  
    protected void onProgressUpdate(Integer... values) {     
            myapp.setAdptr(ResultsAdapter.this);
    }
    @Override
    protected void onPostExecute(Void result){       
        for(int i =0; i < allImageViews.size(); i++){ 
            allImageViews.get(i).setImageBitmap(bitmaps.get(i));
            holder.avatar = allImageViews.get(i);
        }
        if (dialog.isShowing()) {             
            dialog.dismiss();         
            }
    }

 }

static class ViewHolder{
    TextView name, tweet;
    ImageView avatar;
}

}
于 2012-08-11T19:03:43.763 に答える