简体   繁体   中英

get the images from drawable

I am beginner in android development my question is how can i load this image's from drawables to adapter using getResource function and how to get all the items inside drawbles into the adapter class (this is my sample data class where image is loaded to the image adapter)

public class SampleData {

      public static final int SAMPLE_DATA_ITEM_COUNT = 20;

        static int i;

public static ArrayList<Data> generateSampleData() {
    String repeat = " repeat";

    final ArrayList<Data> datas = new ArrayList<Data>();
    for (i = 0; i < SAMPLE_DATA_ITEM_COUNT; i++) {
        Data data = new Data();
        if (i == 0) {
            data.imageUrl = R.drawable.a;
        }
        if (i == 1) {
            data.imageUrl = R.drawable.b;

        }
        if (i == 2) {
            data.imageUrl = R.drawable.c;

        } else if (i == 3) {
            data.imageUrl = R.drawable.d;

        } else if (i == 4) {
            data.imageUrl = R.drawable.e;

        } else if (i == 5) {
            data.imageUrl = R.drawable.f;

        } else if (i == 6) {
            data.imageUrl = R.drawable.g;

        } else if (i == 7) {
            data.imageUrl = R.drawable.h;

        } else if (i == 8) {
            data.imageUrl = R.drawable.i;

        } else if (i == 10) {
            data.imageUrl = R.drawable.j;

        } else if (i == 11) {
            data.imageUrl = R.drawable.k;

        } else if (i == 12) {
            data.imageUrl = R.drawable.l;

        } else if (i == 13) {
            data.imageUrl = R.drawable.m;


        } else if (i == 13) {
            data.imageUrl = R.drawable.m;
        } else if (i == 13) {
            data.imageUrl = R.drawable.o;
        } else if (i == 13) {
            data.imageUrl = R.drawable.p;
        } else if (i == 13) {
            data.imageUrl = R.drawable.q;
        }

        /*data.title = "Gif Card";
        data.description = "Super awesome description";
        Random ran = new Random();
        int x = ran.nextInt(i + SAMPLE_DATA_ITEM_COUNT);
        for (int j = 0; j < x; j++)
            data.description += repeat;*/

        datas.add(data);
    }
    return datas;
}

}

if you consider that the char a in the ascii table is decimal 97 , b is 98 and so on, what you could do is

public static ArrayList<Data> generateSampleData(Context context) {
   String repeat = " repeat";

   final ArrayList<Data> datas = new ArrayList<Data>();
   for (i = 0; i < SAMPLE_DATA_ITEM_COUNT; i++) {
       Data data = new Data();
       data.imageUrl = context.getResources().getIdentifier(String.valueOf((char)(97+i)), "drawable", context.getPackageName());
       datas.add(data);
    }
    return data; 
}

if your drawables follow the same naming convention you won't need the switch or if construct, but just the loop. Of course you need the Context to access the resources, so I changed the signature of your method

If the number of drawables is equal to your SAMPLE_DATA_ITEM_COUNT -

final ArrayList<Data> datas = new ArrayList<Data>();
final int[] drawables = {
      R.drawable.a, R.drawable.b, ...          
};
for (i = 0; i < drawables.length; i++) {
    Data data = new Data();
    data.imageUrl = drawables[i];
    datas.add(data);
}
return datas;

This saves series of ifs / switch statements in each iteration, as well as more expensive calls to the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM