在用于Gridview项目的自定义适配器中实现Glide

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

我是android编程的新手,无法在自定义适配器中正确使用glide(即:with(context)和load protocols)的语法。请让我知道我做错了什么。先感谢您

尝试将上下文更改为view1并创建新上下文

String[] HelmetNames = {"Helmet 1","Helmet 2","Helmet 3","Helmet 4","Helmet 5","Helmet 6"};
        int[] HelmetImages = {R.drawable.a7021906,R.drawable.a7021038,R.drawable.a2111976,R.drawable.a7751025,R.drawable.a3739,R.drawable.a2661082};
       String[] HelmetDetails={"4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf","4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf","4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf","4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf","4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf","4-point ratchet suspension allows wearer to customize height and fit\n • Suspension sits lower on head to reduce pressure and increase security\n • Short brim enhances better upward visibility\n • Slots allow for attachment of safety accessories\n\n\n\nhttps://multimedia.3m.com/mws/media/828564O/3m-head-protection-hard-hats-101-technical-bulletin.pdf"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_headgrid);

            //finding listview
            gridView = findViewById(R.id.gridview);

            CustomAdapter customAdapter = new CustomAdapter();
            gridView.setAdapter(customAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              Toast.makeText(getApplicationContext(),HelmetNames[i],Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(getApplicationContext(),HeadGridItemActivity.class);
                    intent.putExtra("name1",HelmetNames[i]);
                    intent.putExtra("image1",HelmetImages[i]);
                    intent.putExtra("details1",HelmetDetails[i]);
                    startActivity(intent);

                }
            });


        }

        private class CustomAdapter extends BaseAdapter {
            @Override
            public int getCount() {
                return HelmetImages.length;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                View view1 = getLayoutInflater().inflate(R.layout.row_datahead,null);
                //getting view in row_data
                TextView name = view1.findViewById(R.id.helmetname);
                ImageView image = view1.findViewById(R.id.helmetimages);
                Glide.with(context)
                     .load(HelmetImages[i])
                     .into(image);

                name.setText(HelmetNames[i]);
               // image.setImageResource(HelmetImages[i]);
                return view1;



            }
        }
    }
java android android-glide custom-adapter
2个回答
0
投票

我不知道你的错误是什么,但看看如何使用Glide制作自定义适配器和加载图像custom listView

正如@Beyazid在他的回答中提到的,如果要显示一些本地图像,则不需要使用Glide。


0
投票

你为什么用Glide?如果您不打开图像URL,则不需要Glide。

你可以使用它

Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

如果您需要从URL加载照片,裁剪照片,转换等,您可以使用Glide。

Glide.with(yourContext)
    .load(url)
    .apply(cropOptions)
    .transition(withCrossFade())
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.imagenotfound)
    .into(imageView);
© www.soinside.com 2019 - 2024. All rights reserved.