无法从回收视图中的firestore检索数据



我的活动代码:

package com.example.licenta;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
public class ListaPacienti extends AppCompatActivity {

    RecyclerView recyclerView;
    FirebaseFirestore db;
    Myadapter myadapter;
    ArrayList<Pacienti> list;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_pacienti);
        //progressDialog = new ProgressDialog(this);
        //progressDialog.setCancelable(false);
        //progressDialog.setMessage("...");
        //progressDialog.show();
        recyclerView = findViewById(R.id.listapacienti);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        db = FirebaseFirestore.getInstance();
        list = new ArrayList<Pacienti>();
        myadapter = new Myadapter(ListaPacienti.this, list);
        recyclerView.setAdapter(myadapter);

        EventChangeListener();
    }

    private void EventChangeListener() {
        db.collection("Nume")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                        if (error!=null){
                            Log.e("Firestore error", error.getMessage());
                            return;
                        }
                        for (DocumentChange dc: value.getDocumentChanges()){
                            if(dc.getType() == DocumentChange.Type.ADDED){
                                list.add(dc.getDocument().toObject(Pacienti.class));
                            }
                            myadapter.notifyDataSetChanged();
                        }

                    };
                });
    }
}

适配器代码:

package com.example.licenta;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {
    Context context;
    ArrayList<Pacienti> list;
    
    public Myadapter(Context context, ArrayList<Pacienti> list) {
        this.context = context;
        this.list = list;
    }
    @NonNull
    @Override
    public Myadapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new MyViewHolder(v);
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Pacienti pacienti = list.get(position);

        holder.nume.setText(list.get(position).getNume());
        holder.varsta.setText(list.get(position).getVarsta());
        holder.actualizare.setText(String.valueOf(list.get(position).getActualizare()));
        //holder.varsta.setText(String.valueOf(list.varsta));
        //holder.actualizare.setText(String.valueOf(pacienti.actualizare));
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView nume, varsta, actualizare;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nume=itemView.findViewById(R.id.lstnume);
            varsta=itemView.findViewById(R.id.lstvarsta);
            actualizare=itemView.findViewById(R.id.lstactualizare);
        }
    }
}

类代码:

package com.example.licenta;
public class Pacienti {
    public String nume, varsta;
    public long actualizare;
    public Pacienti(){
    }
    public Pacienti(String nume, String varsta, long actualizare) {
        this.nume = nume;
        this.varsta = varsta;
        this.actualizare = actualizare;
    }
    public String getNume() {
        return nume;
    }
    public void setNume(String nume) {
        this.nume = nume;
    }

    public String getVarsta() {
        return varsta;
    }
    public void setVarsta(String varsta) {
        this.varsta = varsta;
    }

    public long getActualizare() {
        return actualizare;
    }

    public void setActualizare(long actualizare) {
        this.actualizare = actualizare;
    }
}

和firestore:

输入图片描述

我不知道我的谷歌服务。Json是问题所在。

{
  "project_info": {
    "project_number": "825557024849",
    "project_id": "listapacienti",
    "storage_bucket": "listapacienti.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:825557024849:android:e17859e27645053a59b870",
        "android_client_info": {
          "package_name": "com.example.licenta"
        }
      },
      "oauth_client": [
        {
          "client_id": "825557024849-v2kacaptlk31v3lbp7gn2pboi70bfda9.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyCGkeG2qA8tSUYGV5dMjKz-RTcJAtXs1dw"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "825557024849-v2kacaptlk31v3lbp7gn2pboi70bfda9.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

应用程序启动,但不显示来自firebase的数据。我尝试了get(),但它不起作用。它是一个应用程序的一部分,所以有可能有这个问题从另一个文件?

您尝试检索actualizare作为long变量,而它是timestamp。尝试更改所有日期到在你的类中:

import java.util.Date;
public class Pacienti {
public String nume, varsta;
public Date actualizare;
public Pacienti(){
}
public Pacienti(String nume, String varsta, Date actualizare) {
    this.nume = nume;
    this.varsta = varsta;
    this.actualizare = actualizare;
}
public String getNume() {
    return nume;
}
public void setNume(String nume) {
    this.nume = nume;
}

public String getVarsta() {
    return varsta;
}
public void setVarsta(String varsta) {
    this.varsta = varsta;
}

public Date getActualizare() {
    return actualizare;
}

public void setActualizare(Date actualizare) {
    this.actualizare = actualizare;
}

}

最新更新