How to use asyncTask to load images from resources

Faisal Asif

Here in this custom adapter, in my list view there is only one imageview. i am loading a list of images in this listview. i want to load new images in asynctask but idont know how to implement asynctask and most the tutorials refers to image downloading but i am using drawable resources, so kindly guide me through this how do i implement asynchronous loading of images to replace image in current visible view.

class MyAdapter extends BaseAdapter {

    private Context context;
    private int images[];

    public MyAdapter(Context context, int images[]) {
        this.context = context;
        this.images = images;
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return images[position];
    }

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

    class MyViewHolder {
        ImageView imageView;

        public MyViewHolder(View v) {
            // TODO Auto-generated constructor stub
            imageView = (ImageView) v.findViewById(R.id.imgView);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        MyViewHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.custom_list, parent, false);

            holder = new MyViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (MyViewHolder) row.getTag();
            row = convertView;
        }
        holder.imageView.setImageResource(images[position]);
        return row;
    }
}

and here is my MainActivity where I am calling MyAdapter to get that list of images.

public class MainActivity extends Activity {

    private ListView listView;
    private MyAdapter myAdapter;

    int[] images = { R.drawable.img001, R.drawable.img002, R.drawable.img003,
            R.drawable.img004, R.drawable.img005, R.drawable.img006,
            R.drawable.img007, R.drawable.img008, R.drawable.img009,
            R.drawable.img010, R.drawable.img011, R.drawable.img012,
            R.drawable.img013, R.drawable.img014, R.drawable.img015,
            R.drawable.img016, R.drawable.img017, R.drawable.img018,
            R.drawable.img019, R.drawable.img020, R.drawable.img021,
            R.drawable.img022, R.drawable.img023, R.drawable.img024,
            R.drawable.img025, R.drawable.img026, R.drawable.img027,
            R.drawable.img028, R.drawable.img029, R.drawable.img030,
            R.drawable.img031, R.drawable.img032, R.drawable.img033,
            R.drawable.img034, R.drawable.img035, R.drawable.img036,
            R.drawable.img037, R.drawable.img038, R.drawable.img039,
            R.drawable.img040, R.drawable.img041, R.drawable.img042,
            R.drawable.img043, R.drawable.img044, R.drawable.img045,
            R.drawable.img046, R.drawable.img047, R.drawable.img048,
            R.drawable.img049, R.drawable.img050, R.drawable.img051,
            R.drawable.img052, R.drawable.img053, R.drawable.img054,
            R.drawable.img055, R.drawable.img056, R.drawable.img057,
            R.drawable.img058, R.drawable.img059, R.drawable.img060,
            R.drawable.img061, R.drawable.img062, R.drawable.img063,
            R.drawable.img064, R.drawable.img065, R.drawable.img066,
            R.drawable.img067, R.drawable.img068, R.drawable.img069,
            R.drawable.img070, R.drawable.img071, R.drawable.img072,
            R.drawable.img073, R.drawable.img074, R.drawable.img075,
            R.drawable.img076, R.drawable.img077, R.drawable.img078,
            R.drawable.img079, R.drawable.img080, R.drawable.img081,
            R.drawable.img082, R.drawable.img083, R.drawable.img084,
            R.drawable.img085, R.drawable.img086, R.drawable.img087,
            R.drawable.img088, R.drawable.img089, R.drawable.img090,
            R.drawable.img091, R.drawable.img092, R.drawable.img093,
            R.drawable.img094, R.drawable.img095, R.drawable.img096,
            R.drawable.img097, R.drawable.img098, R.drawable.img099,
            R.drawable.img100 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
        myAdapter = new MyAdapter(this, images);
        listView = (ListView) findViewById(R.id.imgList);
        listView.setAdapter(myAdapter);
    }
  }
mbmc
dependencies {
    compile 'com.squareup.picasso:picasso:2.3.2'
}

Picasso.with(context).load(R.drawable.<id>).into(holder.imageView);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to load images from resources

From Dev

How to load images from html in the AsyncTask

From Dev

How to load GridView images with AsyncTask?

From Dev

Using Glide for Android, how do I load images from asset and resources?

From Dev

How to return images from asynctask on Android

From Dev

How to load and use images in applets?

From Dev

How to load resources from other JAR file

From Dev

How to load image from embedded resources

From Dev

How to load JsonObjectRequest in an Asynctask?

From Dev

Using AsyncTask to load a lot of images on one layout?

From Dev

Unable to load Images in ListView using AsyncTask

From Dev

How to load images faster from Azure Blob?

From Dev

How to load images from mipmap folder programmatically?

From Dev

How to load images to ImageButton from url

From Dev

How to load Images from a package in java

From Dev

How to load all images from assets?

From Dev

How to load images from a directory on the computer in Python

From Dev

How to load images from a server using plist

From Dev

How to load images to ImageButton from url

From Dev

How to load images from a directory on the computer in Python

From Dev

How to load all the images from one Directory

From Dev

How to use resources in XAML from .resources files (not .resx files)?

From Dev

How to use Resources of from another project in WPF?

From Dev

How to use Resources of from another project in WPF?

From Dev

How to make 10 random images from resources appear on a button?

From Dev

Webkit browsers inconsistently load webpage images/resources

From Dev

How to load alternative strings.xml from resources programmatically

From Dev

How to load qml file from resources, using QtQuick 2 Controls

From Dev

Xamarin Local Images from Resources

Related Related

  1. 1

    how to load images from resources

  2. 2

    How to load images from html in the AsyncTask

  3. 3

    How to load GridView images with AsyncTask?

  4. 4

    Using Glide for Android, how do I load images from asset and resources?

  5. 5

    How to return images from asynctask on Android

  6. 6

    How to load and use images in applets?

  7. 7

    How to load resources from other JAR file

  8. 8

    How to load image from embedded resources

  9. 9

    How to load JsonObjectRequest in an Asynctask?

  10. 10

    Using AsyncTask to load a lot of images on one layout?

  11. 11

    Unable to load Images in ListView using AsyncTask

  12. 12

    How to load images faster from Azure Blob?

  13. 13

    How to load images from mipmap folder programmatically?

  14. 14

    How to load images to ImageButton from url

  15. 15

    How to load Images from a package in java

  16. 16

    How to load all images from assets?

  17. 17

    How to load images from a directory on the computer in Python

  18. 18

    How to load images from a server using plist

  19. 19

    How to load images to ImageButton from url

  20. 20

    How to load images from a directory on the computer in Python

  21. 21

    How to load all the images from one Directory

  22. 22

    How to use resources in XAML from .resources files (not .resx files)?

  23. 23

    How to use Resources of from another project in WPF?

  24. 24

    How to use Resources of from another project in WPF?

  25. 25

    How to make 10 random images from resources appear on a button?

  26. 26

    Webkit browsers inconsistently load webpage images/resources

  27. 27

    How to load alternative strings.xml from resources programmatically

  28. 28

    How to load qml file from resources, using QtQuick 2 Controls

  29. 29

    Xamarin Local Images from Resources

HotTag

Archive