如何将图像从drawable文件夹设置为布局而不是String url

问题描述 投票:2回答:1

我有一个活动类,其中图像显示在2列的网格视图中。我使用以下代码来显示图像

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;
    }


    }
}

但是我想使用drawable文件夹中的图像,而不是使用url。

我怎么能这样做..请帮忙。

提前致谢。

java android android-layout android-recyclerview recycler-adapter
1个回答
3
投票

您需要使用drawable文件夹中的图像创建一个整数数组。

码:

        //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;
        }

您可能需要调整BookDetails类,因为现在您使用的是整数而不是String,您应该更改StaggeredAdapter以从资源而不是从url加载图像。

我希望它对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.