我有一个关于如何将图像从实时数据库链接到每个对象的问题。
我的应用程序中有RecyclerView,其中包含Firebase数据库中的所有对象。
对象包含lampTitle,lampDesc,lampType。
在Firebase存储中,我有3张图像,应在“回收器”视图中的每个对象下显示。 每个图像应与他的对象匹配。 例如。 air_condition.png应该带有AirCondition对象,等等。
我的LightsFragment-包含回收站视图:
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);
}
}
轻量级:
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;
}
}
点亮演示者并查看:
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 ");
}
});
}
}
我得到的结果是:
答案 0 :(得分:1)
无需匹配任何内容,您只需将图片网址存储在每个对象下:
Firebase-root
|
--- AllLights
|
--- AirBag
|
--- lampDesc: "Is it safe..."
|
--- lampTitle: "Air bag Lamp"
|
--- lampType: 1
|
--- lampImageUrl: "Firebase Storage Image Url" //New property added
如您所见,我添加了一个新属性,该属性应包含相应图像的url。最后,您只需使用Glide来显示它。