简体   繁体   中英

How to set Image from drawable folder into the layout instead of String url

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.

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.

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