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

时间:2019-03-25 14:32:54

标签: java android android-glide custom-adapter

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

尝试将上下文更改为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;



            }
        }
    }

2 个答案:

答案 0 :(得分:0)

为什么要使用Glide?如果您不打开图片网址,则不需要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);

答案 1 :(得分:0)

我不知道显示什么错误,但是请在custom listView这里看看如何制作自定义适配器并使用Glide加载图像

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