在android中更新我的适配器的imageView

时间:2014-02-28 13:49:02

标签: java android android-imageview

我想使用AsyncTask来获取缓存或网络中的文章图像。之后将这些图像放在我的适配器的相应imageView中。 这是我的代码:

Article.java

public class Article {

    private String titre = "";
    private String auteur = "";
    private String date = "";
    private String contenu = "";
    private String url = "";
    private Bitmap photo = null;

    public Article() {
    }

    public Article(Context context, String titre, String auteur, String date,
            String url, String contenu) {
        this.titre = titre;
        this.auteur = auteur;
        this.date = date;
        this.contenu = contenu;
        this.url = url;
        Bitmap image = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.no_image);
        this.photo = image;
    }

    public String getTitre() {
        return titre;
    }

    public String getAuteur() {
        return auteur;
    }

    public String getDate() {
        return date;
    }

    public String getUrl() {
        return url;
    }

    public String getContenu() {
        return contenu;
    }

    public Bitmap getPhoto() {
        return photo;
    }

    public void setTitre(String titre) {
        this.titre = titre;
    }

    public void setAuteur(String auteur) {
        this.auteur = auteur;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setContenu(String contenu) {
        this.contenu = contenu;
    }

    public void setPhoto(Bitmap photo) {
        this.photo = photo;
    }
}

ArticleAdapter

public class ArticleAdapter extends BaseAdapter {

    private ArrayList<Article> articles;
    private LayoutInflater mInflater;
    private Resources mResource = null;

    public ArticleAdapter(Context context, ArrayList<Article> articles) {
        mInflater = LayoutInflater.from(context);
        this.articles = articles;
        this.mResource = context.getResources();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return articles.size();
    }

    @Override
    public Object getItem(int pos) {
        // TODO Auto-generated method stub
        return articles.get(pos);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ligne, null);
            holder = new ViewHolder();
            holder.mImage = (ImageView) convertView.findViewById(R.id.photo);
            holder.mAuteur = (TextView) convertView.findViewById(R.id.auteur);
            holder.mTitre = (TextView) convertView.findViewById(R.id.titre);
            holder.mContenu = (TextView) convertView.findViewById(R.id.contenu);
            holder.mDate = (TextView) convertView.findViewById(R.id.date);

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

        Article a = (Article) getItem(position);
        if (a != null) {
            if (a.getPhoto() != null)
                holder.mImage.setImageBitmap(a.getPhoto());
            holder.mAuteur.setText(a.getAuteur());
            holder.mContenu.setText(wrapWord(position));
            holder.mDate.setText(a.getDate());
            holder.mTitre.setText(a.getTitre());
        }

        return convertView;
    }

    private String wrapWord(int pos) {
        String retour = "";
        String val = mResource.getString(R.string.size_article_list);
        int n = Integer.valueOf(val).intValue();
        Article article = (Article) getItem(pos);
        String a = article.getContenu();

        if (a.length() > n)
            retour = a.substring(0, n) + "...";
        else
            retour = a;

        return retour;
    }

    public class ViewHolder {
        private ImageView mImage;
        private TextView mTitre;
        private TextView mContenu;
        private TextView mAuteur;
        private TextView mDate;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用AsyncHttpClient方法从网络获取照片,然后在onSuccess方法中,您将处理ImageView的setter。 阅读有关AsyncHttpClient以及如何使用它来获取照片的信息

例如,您可以为HTTP请求创建类,如下所示

public class MyCLient{
  private static AsyncHttpClient client = new AsyncHttpClient();
  public static void get(String url, RequestParams params, AsyncHttpResponseHandler           responseHandler) {
     client.get(url, params, responseHandler);
 }

 public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
     client.post(url, params, responseHandler);
 }

}

然后在你的适配器中,你将调用get方法来获取如下图像:     MyCLient.get(&#34;你的网址&#34;,null,新的JsonHttpResponseHandler(){             @覆盖             public void onSuccess(JSONArray timeline){                 //读取图像并设置ImageView。             }         });

如果您无法访问适配器内的活动,则只需在适配器类中定义实例变量&#34; Activity myActivity = null;&#34;然后将您的活动作为输入参数传递给适配器承包商并使用它来设置myActivity,然后您可以在onSuccess方法中使用myActivity。

答案 1 :(得分:0)

感谢您的帮助。我发现了我的问题。 我创建了一个我的适配器的interne类和扩展的AsyncTask。现在,如果我在getView上调用它,我可以更新我的imageViews。这是代码

公共类ArticleAdapter扩展了BaseAdapter {

private ArrayList<Article> articles;
private LayoutInflater mInflater;
private Resources mResource = null;
private static final int SIZE = 5 * 1024*1024;
private DiskLruImageCache imageCache = null;
private ViewHolder mViewHolder = null;

public ArticleAdapter(Context context, ArrayList<Article> articles){
    mInflater = LayoutInflater.from(context);
    this.articles = articles;
    this.mResource = context.getResources();
    this.imageCache = new DiskLruImageCache(context, "ArticleList", SIZE, CompressFormat.JPEG, 70);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return articles.size();
}

@Override
public Object getItem(int pos) {
    // TODO Auto-generated method stub
    return articles.get(pos);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public ViewHolder getViewHolder(){
    return mViewHolder;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.ligne, null);
        mViewHolder = new ViewHolder();
        mViewHolder.mImage = (ImageView)convertView.findViewById(R.id.photo);
        mViewHolder.mAuteur = (TextView)convertView.findViewById(R.id.auteur);
        mViewHolder.mTitre = (TextView)convertView.findViewById(R.id.titre);
        mViewHolder.mContenu = (TextView)convertView.findViewById(R.id.contenu);
        mViewHolder.mDate = (TextView)convertView.findViewById(R.id.date);

        convertView.setTag(mViewHolder);
    }else{
        mViewHolder = (ViewHolder)convertView.getTag();
    }

    Article a = (Article)getItem(position);
    if(a != null){
        if(mViewHolder.mBitmap != null){
            a.setPhoto(mViewHolder.mBitmap);
        }
        if(a.getPhoto() != null) mViewHolder.mImage.setImageBitmap(a.getPhoto());
        mViewHolder.mAuteur.setText(a.getAuteur());
        mViewHolder.mContenu.setText(wrapWord(position));
        mViewHolder.mDate.setText(a.getDate());
        mViewHolder.mTitre.setText(a.getTitre());
        mViewHolder.mUrl = a.getUrl();

        new ImageTask().execute(mViewHolder);
    }

    return convertView;
}

private String wrapWord(int pos){
    String retour = "";
    String val = mResource.getString(R.string.size_article_list);
    int n = Integer.valueOf(val).intValue();
    Article article = (Article)getItem(pos);
    String a = article.getContenu();

    if(a.length() > n) retour = a.substring(0, n)+"...";
    else retour = a;

    return retour;
}


public class ViewHolder{
    private ImageView mImage;
    private TextView mTitre;
    private TextView mContenu;
    private TextView mAuteur;
    private TextView mDate;
    private Bitmap mBitmap;
    private String mUrl;
}

public class ImageTask extends AsyncTask<ViewHolder, Void, ViewHolder>{
    Bitmap image = null;
    @Override
    protected ViewHolder doInBackground(ViewHolder... holder) {
        // TODO Auto-generated method stub
        ViewHolder ele = holder[0];
        String key = ele.mUrl.replace("http://", "").replace("/", "_").replace("?", "_").replace(".", "_");
        if(imageCache.containsKey(key)){
            ele.mBitmap = imageCache.getBitmap(key);
        }else{
            try {
                byte[] bytes = IOUtils.toByteArray(new URL(ele.mUrl));
                image = ImageResizer.decodeSampleBitmap(bytes, 400, 400);
                ele.mBitmap = image;
                imageCache.put(key, image);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return ele;
    }

    protected void onPostExecute(ViewHolder holder){
        if(holder.mBitmap != null) holder.mImage.setImageBitmap(holder.mBitmap);
    }

}}