首页 > 解决方案 > 从firestore获取空值到recycleview

问题描述

我已经制作了从 Cloud Firestore 获取数据的通知活动。在我测试应用程序之后。我总是从数据库中得到空值,我检查了调试。我发现数据是从数据库中读取的,但没有在 Notification.java 中设置。这是我的代码:

NotificationActivity.java:

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

        getSupportActionBar().setTitle(R.string.menu_notifications);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        initRecyclerView();
        getNotifications();
        Adview();
    }

    private void initRecyclerView() {
        Log.d(TAG, "initRecyclerView: init recyclerview.");
        mRecyclerView = findViewById(R.id.recyclerv_view);
        if (mNotificationsAdapter == null) {
            mNotificationsAdapter = new NotificationsAdapter(this, mNotifications);
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mNotificationsAdapter);
    }

    private void getNotifications() {
        SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
        String token = prefs.getString("token", "");

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        CollectionReference notificationsCollectionRef = db.collection("notifications").document(token).collection("messages");
        Query query = notificationsCollectionRef
                .orderBy("Date", Query.Direction.DESCENDING);
        query.get().addOnCompleteListener(task -> {

            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Notifications notifications = document.toObject(Notifications.class);
                    mNotifications.add(notifications);
                }
                mNotificationsAdapter.notifyDataSetChanged();
            } else {
                Log.d(TAG,"Query Failed. Check Logs.");
            }
        });
    }

NotificationsAdapter.java:

class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "NotificationsAdapter";
    private ArrayList<Notifications> mNotifications;
    private Context mContext;

    public NotificationsAdapter(Context context, ArrayList<Notifications> notifications) {
        mNotifications = notifications;
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.notifications_listitem, parent, false);

        holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: called.");

        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).title.setText(mNotifications.get(position).getTitle());
            ((ViewHolder) holder).message.setText(mNotifications.get(position).getMessage());
            ((ViewHolder) holder).date_time.setText(mNotifications.get(position).getDate() + "\n" + mNotifications.get(position).getTime());
            ((ViewHolder) holder).parentLayout.setOnClickListener(view -> {
                Log.d(TAG, "onClick: clicked on: " + mNotifications.get(position).getTitle());

                Intent intent = new Intent(mContext, NotificationDetails.class);
                //  intent.putExtra("image_url", mImages.get(position));
                intent.putExtra("shop_name", mNotifications.get(position).getTitle());
                intent.putExtra("message", mNotifications.get(position).getMessage());
                mContext.startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mNotifications.size();
    }

       public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView image;
        TextView title, message, date_time;
        RelativeLayout parentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.shop_image);
            title = itemView.findViewById(R.id.title);
            message = itemView.findViewById(R.id.message);
            date_time = itemView.findViewById(R.id.date_time);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }

通知.java

@IgnoreExtraProperties
public class Notifications {
    private String title;
    private String message;
    private String date;
    private String time;

    public Notifications() {
    }

    public Notifications(String title, String message, String date, String time) {
        this.title = title;
        this.message = message;
        this.date = date;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

标签: javaandroiddatabaseandroid-recyclerviewgoogle-cloud-firestore

解决方案


确保您的 Firestore / firebase 属性和您的 java/Kotlin 数据类属性相同。我的意思是大写字母和所有内容。


推荐阅读