[英]Implementing Glide in a Custom Adapter used for Gridview of items
我是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;
}
}
}
你為什么用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);
我不知道你的錯誤是什么,但看看如何制作自定義適配器並使用Glide
加載圖像自定義listView
正如@Beyazid在他的回答中提到的,如果要顯示一些本地圖像,則不需要使用Glide。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.