Android Studio: ListView c фотографией и текстом
Задача: разместить в виджете ListView картинку из файла + текст. Результат должен выглядеть как то так:

Решение: будем писать свой «адаптер», на входе которого будет массив из id и photo_name (имя файла картинки)
Экран list_photos.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/photo_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="photo_id" />
<TextView
android:id="@+id/photo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="photo_name" />
</LinearLayout>
Код адаптера:
ListView listView = findViewById(R.id.ListPhotos);
// используем адаптер данных
adapter=new PhotosAdapter(this,R.layout.list_photos, arrayList);
listView.setAdapter(adapter);
}
// Пишем свой класс-адаптер
private class PhotosAdapter extends ArrayAdapter<String> {
PhotosAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.list_photos, parent, false);
map=arrayList.get(position);
TextView pid = (TextView) row.findViewById(R.id.photo_id);
TextView pname = (TextView) row.findViewById(R.id.photo_name);
File imgFile = new File(map.get("photo_name"));
pid.setText(map.get("photo_id"));
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iconImageView = (ImageView) row.findViewById(R.id.imgv);
iconImageView.setImageBitmap(myBitmap);
return row;
}
}