Skip to content
Advertisement

Can you Display an image that is part of firebase database on Android Studio?

I want to display my firebase database but I want each entry to have an image attached to it. Is it possible to have the variable type “Image” in my Exercise Class? If so how would display it when I run it on my app. I can display all the Strings but when I tried to display the images it would crash.

DATABASE EXAMPLE

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Exercise exercise = list.get(position);
    holder.exerciseName.setText(exercise.getExerciseName());
    holder.muscleGroupName.setText(exercise.getMuscleGroupName());
    holder.equipmentName.setText(exercise.getEquipmentName());
    holder.imageView.setImageResource(exercise.getImage());
}

public class Exercise {
    String exerciseName, muscleGroupName, equipmentName;
    Image image;

    public Image getImage() {
        return image;
    }

    public String getExerciseName() {
        return exerciseName;
    }

    public String getMuscleGroupName() {
        return muscleGroupName;
    }

    public String getEquipmentName() {
        return equipmentName;
    }
}

What I’m trying to display

Advertisement

Answer

on your build.gradle(Module: app) under dependencies add this library:

dependencies {

 
 //other dependencies 
 
 //GLide
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

re write your class as:

public class Exercise {

        String exerciseName, muscleGroupName, equipmentName,image;

        public String getImage() {
            return image;
        }

        public String getExerciseName() {
            return exerciseName;
        }

        public String getMuscleGroupName() {
            return muscleGroupName;
        }

        public String getEquipmentName() {
            return equipmentName;
        }

    }

Use it this way:

Exercise exercise = list.get(position);
                    holder.exerciseName.setText(exercise.getExerciseName());
                    holder.muscleGroupName.setText(exercise.getMuscleGroupName());
                    holder.equipmentName.setText(exercise.getEquipmentName());
                    Glide.with(holder.imageView)
                            .load(exercise.getImage())
                            .into(holder.imageView);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement