简体   繁体   English

无法从 Firebase Recycler Adapter android java 中的模型中检索数据数组项

[英]Can't retrieve data array item from model in Firebase Recycler Adapter android java

I have problem to retrieve data which one of item has type arrayList into recyclerView.我有问题将其中一项的数据类型为 arrayList 检索到 recyclerView。 Previously, I've asked and discussed about the right model for url_photo_collage on How to retrieve array data from children node in Firebase Realtime Database Java?以前,我已经询问并讨论了url_photo_collage的正确模型如何从 Firebase 实时数据库 Java 中的子节点检索数组数据? (Firebase Recycler Adapter) but the problem is my recyclerview has showed nothing . (Firebase Recycler Adapter)但问题是我的 recyclerview 什么也没显示。

This is my structure database was described in this Picture这是我在这张图片中描述的结构数据库

在此处输入图片说明

There are classes I 've tried which followed with the suggestion before我尝试过一些课程,之前遵循了建议

In the model class, I've changed the data type of url_photo_college to be List在模型类中,我将url_photo_college的数据类型url_photo_college为 List

DolanItemClass.Java DolanItemClass.Java

package co.id.roningrum.firebasearrayimage;

import java.util.List;

public class DolanItemClass {
    private String name_tourism;
    private String location_tourism;
    private String info_tourism;
    private String telepon;
    private String url_photo;
    private List<String> url_photo_collage;
    private double lat_location_tourism;
    private double lng_location_tourism;

    public DolanItemClass() {
        //constructor untuk panggilan ke DataSnapshot.getValue
    }

    public DolanItemClass(String name_tourism, String location_tourism, String info_tourism, String telepon, String url_photo, List<String> url_photo_collage, double lat_location_tourism, double lng_location_tourism) {
        this.name_tourism = name_tourism;
        this.location_tourism = location_tourism;
        this.info_tourism = info_tourism;
        this.telepon = telepon;
        this.url_photo = url_photo;
        this.url_photo_collage = url_photo_collage;
        this.lat_location_tourism = lat_location_tourism;
        this.lng_location_tourism = lng_location_tourism;
    }

    public List<String> getUrl_photo_collage() {
        return url_photo_collage;
    }

    public String getName_tourism() {
        return name_tourism;
    }

    public void setName_tourism(String name_tourism) {
        this.name_tourism = name_tourism;
    }

    public String getLocation_tourism() {
        return location_tourism;
    }

    public void setLocation_tourism(String location_tourism) {
        this.location_tourism = location_tourism;
    }

    public String getInfo_tourism() {
        return info_tourism;
    }

    public void setInfo_tourism(String info_tourism) {
        this.info_tourism = info_tourism;
    }

    public String getTelepon() {
        return telepon;
    }

    public void setTelepon(String telepon) {
        this.telepon = telepon;
    }

    public String getUrl_photo() {
        return url_photo;
    }

    public void setUrl_photo(String url_photo) {
        this.url_photo = url_photo;
    }

    public double getLat_location_tourism() {
        return lat_location_tourism;
    }

    public void setLat_location_tourism(double lat_location_tourism) {
        this.lat_location_tourism = lat_location_tourism;
    }

    public double getLng_location_tourism() {
        return lng_location_tourism;
    }

    public void setLng_location_tourism(double lng_location_tourism) {
        this.lng_location_tourism = lng_location_tourism;
    }
}

because of showing recyclerview on DetailActivity, I created the new class viewholder especially for url_photo_college item由于在 DetailActivity 上显示 recyclerview,我特别为 url_photo_college 项目创建了新的类 viewholder

DolanImageViewHolder.class DolanImageViewHolder.class


import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class DolanImageViewHolder extends RecyclerView.ViewHolder {
    private final ImageView imgDetailData;
    private List<String> urlPhotoArray;

    public DolanImageViewHolder(@NonNull View itemView) {
        super(itemView);
        imgDetailData = itemView.findViewById(R.id.img_detail_item_data);
    }
    public void showImageArray(DolanItemClass dolanItemClass){
        urlPhotoArray = dolanItemClass.getUrl_photo_collage();
        Glide.with(itemView.getContext()).load(urlPhotoArray).into(imgDetailData);
        Log.d("Pesan", "List"+urlPhotoArray);
    }
}

And the url_photo_college item will show in Detail Activity.并且url_photo_college项目将显示在详细活动中。 But unfortunately rectclerview show nothing.但不幸的是 rectclerview 什么也没显示。 It show through method show data它通过方法显示数据显示

DetailTouristActivity.class DetailTouristActivity.class

package co.id.roningrum.firebasearrayimage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class DetailTouristListAct extends AppCompatActivity {
    private ImageView imgDetailData;
    private TextView tvNamaDetail, tvAlamatDetail, tvDescDetail;
    private RecyclerView rvImageDetailCollages;

    private DatabaseReference databaseReference;
    private ValueEventListener valueEventListener;
    private FirebaseRecyclerAdapter<DolanItemClass, DolanImageViewHolder> firebaseRecyclerAdapter;
    public static final String EXTRA_WISATA_KEY = "tourist_key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_tourist_list);

        String touristKey = getIntent().getStringExtra(EXTRA_WISATA_KEY);
        if(touristKey == null){
            throw new IllegalArgumentException("Must pass Extra");
        }

        imgDetailData = findViewById(R.id.img_detail_data);
        tvNamaDetail = findViewById(R.id.tv_name_detail);
        tvAlamatDetail = findViewById(R.id.tv_info_tourism_detail);
        tvDescDetail = findViewById(R.id.tv_address_detail);
        rvImageDetailCollages = findViewById(R.id.rv_photo_collage);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Tourism").child(touristKey);

        rvImageDetailCollages.setLayoutManager(new GridLayoutManager(this, 2));
        LoadDetailData();

    }

    private void ShowCollage() {
        Query query = databaseReference.child("url_photo_collage");
        FirebaseRecyclerOptions<DolanItemClass> options = new FirebaseRecyclerOptions.Builder<DolanItemClass>()
                .setQuery(query, DolanItemClass.class)
                .build();
        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DolanItemClass, DolanImageViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull DolanImageViewHolder dolanImageViewHolder, int i, @NonNull DolanItemClass dolanItemClass) {
                dolanImageViewHolder.showImageArray(dolanItemClass);
            }

            @NonNull
            @Override
            public DolanImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new DolanImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_data, parent, false));
            }
        };
        firebaseRecyclerAdapter.notifyDataSetChanged();
        rvImageDetailCollages.setAdapter(firebaseRecyclerAdapter);
    }

    private void LoadDetailData() {
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                DolanItemClass dolanItemClass = dataSnapshot.getValue(DolanItemClass.class);
                tvNamaDetail.setText(dolanItemClass.getName_tourism());
                tvAlamatDetail.setText(dolanItemClass.getLocation_tourism());
                tvDescDetail.setText(dolanItemClass.getInfo_tourism());
                Glide.with(getApplicationContext()).load(dolanItemClass.getUrl_photo()).into(imgDetailData);
                ShowCollage();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
}

    @Override
    protected void onStart() {
        super.onStart();
        LoadDetailData();

        if(firebaseRecyclerAdapter!=null){
            firebaseRecyclerAdapter.startListening();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        databaseReference.removeEventListener(valueEventListener);

    }
}

The output goal is recyclerview can show data eventhought the item data type is List输出目标是 recyclerview 可以显示数据即使项目数据类型是 List

Here's the error这是错误

 urlPhotoArray = dolanItemClass.getUrl_photo_collage();

You have to call the index from the List by adding .get(0) if you want the first element.如果需要第一个元素,则必须通过添加 .get(0) 来调用 List 中的索引。

Your code should be你的代码应该是

urlPhotoArray = dolanItemClass.getUrl_photo_collage().get(0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FireBase Recycler Adapter 未从 FireBase 检索数据 - FireBase Recycler Adapter doesn't retrieve data from FireBase 如何从Firebase Realtime Database Java中的子节点检索数组数据? (Firebase回收器适配器) - How to retrieve array data from children node in Firebase Realtime Database Java? (Firebase Recycler Adapter) 无法从 firebase 数据库 android 中检索数据,回收站视图 - Unable to retrieve data from firebase database android, recycler view #Askfirebase在Android的Firebase回收器适配器中获取以前的项目值(POJO) - #Askfirebase Get the previous item values(POJO) in firebase recycler adapter in android 通过使用带有 .get(position) 的回收器视图适配器,我无法从回收器视图 java 中的 edittext 获取文本 - I can't get the text from edittext in recycler view java by using recycler view adapter with .get(position) 如何从 Firebase 检索数据到 ListView(使用自定义数组适配器) - How to retrieve data from Firebase into a ListView (with custom Array Adapter) 无法从 android studio 3.5 上的 firebase 检索数据 - can't retrieve data from firebase on android studio 3.5 无法在 Android Studio 中从 Firebase 实时数据库检索数据 - Can't retrieve data from Firebase Realtime db in Android Studio 如何将数据从 FireBase 检索到我的适配器而不是关键 - How can I retrieve data from FireBase to my adapter not the key Firebase Recycler适配器不显示数据,但加载图像 - Firebase Recycler adapter doesn't show data, but load images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM