如何在GridView的ImageView中设置不同类型的图像数据
Got it, let's tackle this problem step by step. Here's how you can implement the logic to set the appropriate content for your CircleImageView based on the data types, plus some performance optimizations for your adapter:
Step 1: Add an Image Loading Library
To handle network image loading efficiently (and avoid writing messy network code yourself), use a library like Glide (or Picasso). For Glide, add this dependency to your module-level build.gradle:
dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0' }
Step 2: Modified Adapter Code
Here's the updated NameListingAdapter with the required image logic and fixes for view reuse (your original code wasn't reusing convertView, which hurts scrolling performance):
private ArrayList<UserListingModel> dataModel; Context context; private static LayoutInflater inflater = null; public NameListingAdapter(Context context, ArrayList<UserListingModel> models) { this.context = context; this.dataModel = models; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return dataModel.size(); } @Override public UserListingModel getItem(int position) { return dataModel.get(position); } @Override public long getItemId(int position) { return position; } public class Holder { TextView tv; CircleImageView img; } @Override public View getView(final int position, View convertView, ViewGroup parent) { Holder holder; View rowView; // Reuse existing views to boost performance if (convertView == null) { rowView = inflater.inflate(R.layout.main_name_list_items, null); holder = new Holder(); holder.tv = rowView.findViewById(R.id.textView1); holder.img = rowView.findViewById(R.id.profile_image); rowView.setTag(holder); } else { rowView = convertView; holder = (Holder) rowView.getTag(); } UserListingModel currentItem = dataModel.get(position); // Set TextView content holder.tv.setText(currentItem.getName()); // Handle ImageView content with priority logic TextDrawable localDrawable = currentItem.getImg(); String imageUrl = currentItem.getImageurl(); if (localDrawable != null) { // Priority 1: Use the TextDrawable if it exists holder.img.setImageDrawable(localDrawable); } else if (imageUrl != null && imageUrl.startsWith("http")) { // Priority 2: Load network image if URL is valid Glide.with(context) .load(imageUrl) .placeholder(R.drawable.default_profile_placeholder) // Optional: Add a placeholder .error(R.drawable.error_profile_image) // Optional: Add an error fallback .into(holder.img); } else { // Fallback: Set a default image if neither data is available holder.img.setImageResource(R.drawable.default_profile); } rowView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(context, DashBoardMain.class); i.putExtra("Name", currentItem.getName()); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }); return rowView; }
Key Details Explained:
- View Reuse: We check if
convertViewis null before inflating a new view—this cuts down on memory usage and makes scrolling smoother. - Image Priority Logic:
- First, we check if
getImg()returns a non-nullTextDrawableand use that immediately. - If no local drawable exists, we validate that
imageurlis a valid network URL (starts with "http") and use Glide to load it. - A fallback default image is set if neither data type is available—replace the drawable IDs with your own assets.
- First, we check if
- Fixed
getItem(): Changed it to return the actualUserListingModelinstead of the position, which is more standard and useful for future changes.
Alternative: Using Picasso
If you prefer Picasso over Glide, replace the Glide code block with this:
Picasso.get() .load(imageUrl) .placeholder(R.drawable.default_profile_placeholder) .error(R.drawable.error_profile_image) .into(holder.img);
And add Picasso's dependency to your build.gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
内容的提问来源于stack exchange,提问作者NIraj Kumar

