0

I'm trying to handle an image loading at the background.

Now, I've look at the next link - here

And I've got few things I don't understand -

1) I've made the next CursorAdapter for the listview items-

    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;


}

Now i wnat to use the info from the link.

But i don't understand - What i need to pass into the and what to AsyncTask leave at CursorAdapter?

Also the sample code uses -

.execute(holder);

Can't I call to the AsyncTask like this -

new AsyncTask().execute();

And how and where should i call the AsyncTask, I don't understand it?

Thanks for any kind of help

4this
  • 759
  • 4
  • 13
  • 27
  • You may want to see my own answer to my question:https://stackoverflow.com/questions/44216331/image-thumbnails-not-setting-correctly/44528936#44528936 – Pushan Gupta Jun 13 '17 at 18:37

3 Answers3

0

You could always use an external lib like Universal-Image-Loader or Picasso to achieve what you are trying to do =)

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
0

Take a look at AsyncTask. You must Override doInBackground method. You may define a constructor to supply view in which you want to put downloaded image.

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);
    }
}
Marius
  • 810
  • 7
  • 20
0

if you write your asyntask method I can say how can you use it, If it need to string value you can use like this:

new your_async(context).execute(url) ;

But in my advice : you should use lazyadapter to use bitmaps on listview because there is a mermory issue if you do not pay attention properties of images. here is link : stackoverfow

Community
  • 1
  • 1
CompEng
  • 7,161
  • 16
  • 68
  • 122