【问题标题】:Android - Listview adapter with asynctask to load imagesAndroid - 使用异步任务加载图像的 Listview 适配器
【发布时间】:2014-03-21 15:53:10
【问题描述】:

我正在尝试在后台处理图像加载。

现在,我查看下一个链接 - here

而且我有一些我不明白的东西 -

1) 我为列表视图项制作了下一个 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?

感谢您的帮助

【问题讨论】:

标签: android listview android-asynctask


【解决方案1】:

您始终可以使用 Universal-Image-Loader 或 Picasso 等外部库来实现您想要做的事情 =)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      new your_async(context).execute(url) ;
      

      但在我的建议中:您应该使用lazyadapter 在listview 上使用位图,因为如果您不注意图像的属性,则会出现内存问题。 这是链接:stackoverfow

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-07
        • 2016-01-26
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多