Android - Listview适配器与asynctask加载图像



我正在尝试处理在背景加载图像。

现在,我已经看了下一个链接-这里

我有几件事不明白-

1)我已经为listview项目做了下一个CursorAdapter -

    public class ChatCursorAdapter extends CursorAdapter implements OnClickListener {

        public ChatCursorAdapter(Context context, Cursor c) {
            super(context, c, 0);
        }
        @Override
        public int getCount() {
            return getCursor() == null ? 0 : super.getCount();
        }
        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getItemViewType(int _position) {
            Cursor cursor = (Cursor) getItem(_position);
            return getItemViewType(cursor);
        }
          private int getItemViewType(Cursor cursor) {
                String sender = cursor.getString(2);
                   SharedPreferences userPref = PreferenceManager
                            .getDefaultSharedPreferences(MainChat.this);

                        String saveUser =   userPref.getString("user", "");
                        if (saveUser.equalsIgnoreCase(sender)){
                            return 0;
                        }else{
                            return 1;
                        }
          }
    @Override
        public void bindView(View view, Context context, Cursor cursor) {
            holder = (ViewHolder) view.getTag();
                holder.mesg.setText(getSmiledText(MainChat.this,msg));
            holder.mesg2.setText(getSmiledText(MainChat.this,msg2));
                    holder.myImage.setTag(picPath);
             holder.myImage.setImageBitmap(setImageToImageView(picPath));
}

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            ViewHolder holder = new ViewHolder();
            View itemLayout = null;
            switch(getItemViewType(cursor)){
            case 0:
                itemLayout = getLayoutInflater().inflate(R.layout.msg_item1,parent, false);
                break;
            case 1:
                itemLayout =  getLayoutInflater().inflate(R.layout.msg_item13, parent,false);
                break;
            }

            itemLayout.setTag(holder);
            holder.mesg = (TextView) itemLayout.findViewById(R.id.text_start);
            holder.mesg2 = (TextView) itemLayout.findViewById(R.id.text_end);
            holder.myImage = (ImageView) itemLayout.findViewById(R.id.imageView_msgpic);
            return itemLayout;

}

现在我想使用链接中的信息

但我不明白-我需要传递什么和什么AsyncTask离开在CursorAdapter?

示例代码也使用-

.execute(holder);

我不能像这样调用AsyncTask -

new AsyncTask().execute();

以及如何和在哪里我应该调用AsyncTask,我不明白它?

感谢您的帮助

您总是可以使用外部库,如Universal-Image-Loader或Picasso来实现您想要做的=)

看看AsyncTask。你必须重写doInBackground方法。您可以定义一个构造函数来提供想要放置下载图像的视图。

public class ImageDownloader extends AsyncTask<String, Void, List<Bitmap>> {
private ImageView ivImageHolder;
private Context context;
public ImageDownloader(Context context, ImageView imageHolder) {
    this.ivImageHolder = imageHolder;
    this.context = context;
}
...
@Override
protected List<Bitmap> doInBackground(String... params) {
//This happens in background
    List<Bitmap> bitmaps = new ArrayList<Bitmap>();
    for (String url : params) {
        Bitmap bitmap = DownloadImage(url);
        bitmaps.add(bitmap);
    }
    return bitmaps;
}
....
private Bitmap DownloadImage(String URL) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;
}
...
private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");
    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;
}
@Override
protected void onPostExecute(List<Bitmap> bitmaps) {
    super.onPostExecute(bitmaps);
    for (int i = 0; i < bitmaps.size(); i++) {
        final Bitmap bitmap = bitmaps.get(i);
        ivImageHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ImageViewActivity(context, bitmap).show();
            }
        });
        // iv.setImageBitmap(bitmap);
        ivImageHolder.setImageBitmap(bitmap);
        ivImageHolder.setVisibility(ImageView.VISIBLE);
    }
}

如果你写你的asyntask方法,我可以说你怎么能使用它,如果它需要字符串值你可以这样使用:

new your_async(context).execute(url) ;

但是在我的建议:你应该使用lazyadapter在listview上使用位图,因为如果你不注意图像的属性,会有内存问题。这里是链接:stackoverflow

相关内容

  • 没有找到相关文章

最新更新