Android新手求助:AddActivity传图片至MainActivity列表不显示问题
Hey there! Let's figure out why your images aren't showing up in the ListView. I spotted two key issues in your code, plus we need to make sure your adapter is handling the image data correctly. Here's how to fix it step by step:
1. Fix the variable name typo in AddActivity
Looking at your AddActivity code, you declared:
byte[] imageDat;
But later in onActivityResult and the button click listener, you're using imageData (notice the extra 'a' at the end). This means you're trying to put an undefined variable into the intent—so the image data being passed to MainActivity is actually null!
Just rename the variable to match:
byte[] imageData; // Replace imageDat with imageData
2. Make sure your ListView item layout has an ImageView
First, check your list item layout (e.g., res/layout/list_item.xml). It needs an ImageView to display the image. Example layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <ImageView android:id="@+id/item_image" android:layout_width="64dp" android:layout_height="64dp" android:scaleType="centerCrop"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginStart="16dp"> <TextView android:id="@+id/item_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/> <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@android:color/darker_gray"/> </LinearLayout> </LinearLayout>
3. Update your ListView Adapter to display the image
Your adapter needs to convert the byte[] image data back into a Bitmap and set it to the ImageView. Here's an example of a corrected BaseAdapter (adjust if you're using RecyclerView.Adapter):
public class EmployeeAdapter extends BaseAdapter { private Context mContext; private List<Employee> mEmployeeList; public EmployeeAdapter(Context context, List<Employee> employeeList) { mContext = context; mEmployeeList = employeeList; } @Override public int getCount() { return mEmployeeList.size(); } @Override public Object getItem(int position) { return mEmployeeList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; // Reuse views to save memory if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.itemImage = convertView.findViewById(R.id.item_image); holder.itemId = convertView.findViewById(R.id.item_id); holder.itemName = convertView.findViewById(R.id.item_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // Get current employee data Employee currentEmp = mEmployeeList.get(position); holder.itemId.setText(String.valueOf(currentEmp.getId())); holder.itemName.setText(currentEmp.getName()); // Convert byte[] to Bitmap and set to ImageView byte[] imageBytes = currentEmp.getImgg(); if (imageBytes != null && imageBytes.length > 0) { Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); holder.itemImage.setImageBitmap(bitmap); } else { // Set a default image if no image was selected holder.itemImage.setImageResource(R.drawable.ic_default_image); } return convertView; } // ViewHolder pattern to avoid repeated findViewById calls static class ViewHolder { ImageView itemImage; TextView itemId; TextView itemName; } }
4. Quick check in MainActivity
Make sure you're initializing the adapter correctly with your list of employees:
// Example initialization in MainActivity's onCreate list = new ArrayList<>(); adapter = new EmployeeAdapter(this, list); ListView listView = findViewById(R.id.your_listview_id); listView.setAdapter(adapter);
Bonus: Avoid OutOfMemoryErrors (Optional)
If you're working with large images, converting byte[] directly to Bitmap might cause memory issues. For beginners, you can add a quick resize to the Bitmap:
// Replace the Bitmap decode line with this Bitmap originalBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, 64, 64, true); holder.itemImage.setImageBitmap(resizedBitmap); originalBitmap.recycle(); // Free up memory
That should get your images showing up in the ListView! Let me know if you run into any other issues.
内容的提问来源于stack exchange,提问作者ayah AH

