2

I have one Activity Class where Image are displayed in Grid view of 2 column. I use the following code to display the images

public class BooksPage extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    String image[] = {  "http://ecx.images-amazon.com/images/I/51cGqWqQPJL._SX258_BO1,204,203,200_.jpg"
            ,"http://ecx.images-amazon.com/images/I/91WAYiiPwML.jpg"
            ,"http://ecx.images-amazon.com/images/I/51CEndEnxCL._SX383_BO1,204,203,200_.jpg"
            ,"https://s3-ap-southeast-1.amazonaws.com/prod-textnook/bookimages/9788185749815.jpg"
            ,"http://ecx.images-amazon.com/images/I/51DyYdNU5mL.jpg"
            ,"http://ecx.images-amazon.com/images/I/61yhyD3HvkL.jpg"
            ,"http://www.skkatariaandsons.com/Best%20Sellers/978-81-85749-73-0.jpg"
            ,"http://ecx.images-amazon.com/images/I/81FGjPtdvnL.jpg"};
    String stream[]= {"MSC Physics","MSC Chemistry","MSC Biology",
            "MSC Geology","BBS|BBA","CA","Bachelor","+2 Science"};
    TextView navName,navEmail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books_page);
       


        

        RecyclerView favPlaces = (RecyclerView) findViewById(R.id.favPlaces);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        favPlaces.setLayoutManager(layoutManager);
        favPlaces.setHasFixedSize(true);
        ArrayList<BookDetails> placeList = getPlaces();

        StaggeredAdapter staggeredAdapter = new StaggeredAdapter(this,placeList);
        favPlaces.setAdapter(staggeredAdapter);
        navName.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("name","null"));
        navEmail.setText(PreferenceManager.getDefaultSharedPreferences(this).getString("email","null"));
    }
    private ArrayList<BookDetails> getPlaces() {
        ArrayList<BookDetails> details = new ArrayList<>();
        for (int index=0; index<image.length;index++){
            details.add(new BookDetails(image[index],stream[index]));
        }
        return details;
    }


    }
}

But instead of using url, I would like to use the image from the drawable folder.

How can I do that..Please help.

Thanks in advance.

Bir Nepali
  • 429
  • 1
  • 8
  • 20
  • Possible duplicate of [How to write an array which stores the images from the drawable folder?](https://stackoverflow.com/questions/32389904/how-to-write-an-array-which-stores-the-images-from-the-drawable-folder) – ʍѳђઽ૯ท Aug 31 '18 at 18:37

1 Answers1

3

You need create an array of integers with the images in the drawable folder.

Code:

        //Array of images from drawable folder
        int images[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,...};

        //Your method still working without problem.
        private ArrayList<BookDetails> getPlaces() {
            ArrayList<BookDetails> details = new ArrayList<>();
            for (int index = 0; index < images.length; index++){
                details.add(new BookDetails(image[index],stream[index]));
            }
            return details;
        }

You maybe need adapt your BookDetails class because now you are using an integer instead of String and you should change StaggeredAdapter to load the image from resource and not from url.

I hope it helps you.

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8