I have a question about how to link images to each object from the real-time database.
I have RecyclerView in my app that contains all objects from Firebase database.
The Object contain lampTitle, lampDesc, lampType.
In firebase storage I have 3 images that should be shown under each object in Recycler view. Each image should match his object. For example. air_conditioning.png should be with AirCondition object, etc...
My LightsFragment - contain recycler view:
public class LightsFragment extends Fragment implements LightsPresenter {
private RecyclerView RVLights;
private ArrayList<Light> lights = new ArrayList<>();
private Light light;
private LightsAdapter lightsAdapter;
private LightsView presenter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lights, container, false);
presenter = new LightsView(lights, light, this);
RVLights = view.findViewById(R.id.RVLights);
presenter.loadData();
return view;
}
@Override
public void setAdapter() {
lightsAdapter = new LightsAdapter(getActivity(), lights);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
RVLights.setLayoutManager(layoutManager);
RVLights.setAdapter(lightsAdapter);
}
LightsAdapter:
public class LightsAdapter extends RecyclerView.Adapter<LightsAdapter.LightsViewHolder> {
private Context context;
private ArrayList<Light> lights;
public LightsAdapter(Context context, ArrayList<Light> lights) {
this.context = context;
this.lights = lights;
}
@NonNull
@Override
public LightsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lights_item, parent, false);
return new LightsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull LightsViewHolder holder, int position) {
final Light light = lights.get(position);
holder.TVlightTitle.setText(light.getLampTitle());
holder.TVLightDesc.setText(light.getLampDesc());
holder.TVLightType.setText(String.valueOf(light.getLampType()));
}
@Override
public int getItemCount() {
return lights.size();
}
public class LightsViewHolder extends RecyclerView.ViewHolder {
private TextView TVlightTitle;
private TextView TVLightDesc;
private TextView TVLightType;
private ImageView IVLightImage;
public LightsViewHolder(@NonNull View itemView) {
super(itemView);
TVlightTitle = itemView.findViewById(R.id.lightTitle);
TVLightDesc = itemView.findViewById(R.id.lightDesc);
TVLightType = itemView.findViewById(R.id.lightType);
IVLightImage = itemView.findViewById(R.id.lightImage);
}
}
Light class:
public class Light {
private int lampType;
private String lampTitle;
private String lampDesc;
public Light(String lampDesc, String lampTitle, int lampType) {
this.lampType = lampType;
this.lampTitle = lampTitle;
this.lampDesc = lampDesc;
}
public Light() {
}
public int getLampType() {
return lampType;
}
public void setLampType(int lampType) {
this.lampType = lampType;
}
public String getLampTitle() {
return lampTitle;
}
public void setLampTitle(String lampTitle) {
this.lampTitle = lampTitle;
}
public String getLampDesc() {
return lampDesc;
}
public void setLampDesc(String lampDesc) {
this.lampDesc = lampDesc;
}
}
Lights presenter and view:
public interface LightsPresenter {
void setAdapter();
}
public class LightsView {
private ArrayList<Light> lights = new ArrayList<>();
private Light light;
private FirebaseDatabase db;
private DatabaseReference allLights;
private LightsPresenter lightsPresenter;
public LightsView(ArrayList<Light> lights, Light light, LightsPresenter lightsPresenter) {
this.lights = lights;
this.light = light;
this.lightsPresenter = lightsPresenter;
}
public void loadData() {
db = FirebaseDatabase.getInstance();
allLights = db.getReference("AllLights");
allLights.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
light = dataSnapshot1.getValue(Light.class);
lights.add(light);
Log.i("LightsDataLoaded", "true " + light.getLampTitle());
}
lightsPresenter.setAdapter();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("LightsDataLoaded", "false ");
}
});
}
}
That what I have in result:
There is no need to match something, you should simply store the image url under each object:
Firebase-root
|
--- AllLights
|
--- AirBag
|
--- lampDesc: "Is it safe..."
|
--- lampTitle: "Air bag Lamp"
|
--- lampType: 1
|
--- lampImageUrl: "Firebase Storage Image Url" //New property added
As you can see I have added a new property that should hold the url of the corresponding image. In the end, you can simply use Glide to display it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.