RecyclerView(FirestoreRecyclerOptions)出现在活动的onCreate中,但在按下后退按钮时为空

时间:2019-11-18 11:57:43

标签: android firebase android-recyclerview google-cloud-firestore firebaseui

请注意,我已经在我的onStart方法中包含了adapter.startListening()方法。

public class Notifications extends AppCompatActivity {
        private static final String TAG = "<< Notifications >>";
        public static final String REQUEST_KEY = "notification request";
        private NotificationAdapter notificationAdapter;

        private RecyclerView notifyRecyclerview;
        private Request request;
        private Request notificationRequest;
        private Query query;

        private FirebaseAuth mAuth;
        private FirebaseUser currentUser;
        private FirebaseFirestore db = FirebaseFirestore.getInstance();
        private CollectionReference requestsRef = db.collection("Users");

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_notifications);
            //Firebase initialization
            mAuth = FirebaseAuth.getInstance();
            currentUser = mAuth.getCurrentUser();

            Toolbar setup_toolbar = findViewById(R.id.notification_toolbar);
            setup_toolbar.setTitle("Notifications");

            setSupportActionBar(setup_toolbar);

            if (getSupportActionBar() != null) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }

            // Recyclerview initialization
            setupRecyclerview();

            // An onclick listener for the individual clicked card
            notificationAdapter.setOnItemCardClickListener(request -> {
                Intent notifyIntent = new Intent(this, NotificationItem.class);
                notifyIntent.putExtra(REQUEST_KEY, request);
                startActivity(notifyIntent);
            });
        }

        @Override
        protected void onStart() {
            super.onStart();
            Log.d(TAG, "onStart: StartListening on the adapter");


            //We must initialize listening to changes here
            notificationAdapter.startListening();
        }


        @Override
        protected void onStop() {
            super.onStop();
            Log.d(TAG, "onStop: StopListening");
            //We must as well stop listening for changes when our app is in background to avoid resource wastage
            notificationAdapter.stopListening();
        }

        private void setupRecyclerview() {
            Log.d(TAG, "setupRecyclerview: ");
            //Todo find a proper way to query the items per per the sort using latest item by current timestamp
            query = requestsRef.document(currentUser.getUid()).collection("Requests").orderBy("date", Query.Direction.ASCENDING);

            FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
                    .setQuery(query, Request.class)
                    .build();

            notificationAdapter = new NotificationAdapter(options);

            notifyRecyclerview = findViewById(R.id.notification_recy);
            notifyRecyclerview.setHasFixedSize(true);
            notifyRecyclerview.setLayoutManager(new LinearLayoutManager(this));
            notifyRecyclerview.setAdapter(notificationAdapter);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
                case android.R.id.home:

                    this.finish();
                    return true;

                default:
                    return super.onOptionsItemSelected(item);
            }

        }
    }

This is my firebase firestore database subcollection.Am querring the database from the User collection/userID document/Requests collection.then getting all the documents from here to populate my recyclerview

这是我的适配器活动,我在另一个适配器上遵循了相同的实现,并且一切正常,但是我真的不明白为什么它会失败

 public class NotificationAdapter extends FirestoreRecyclerAdapter<Request, NotificationAdapter.NotificationViewHolder> {

    Listener listener;
    /**
     * Create a new RecyclerView adapter that listens to a Firestore Query.  See {@link
     * FirestoreRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public NotificationAdapter(@NonNull FirestoreRecyclerOptions<Request> options) {
        super(options);
    }

    @NonNull
    @Override
    public NotificationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        SingleNotificationItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.single_notification_item,parent,false);
        return new NotificationViewHolder(binding);
    }

    @Override
    protected void onBindViewHolder(@NonNull NotificationViewHolder holder, int position, @NonNull Request model) {
        holder.binding.setRequests(model);
        holder.binding.notifyCardItem.setOnClickListener(v -> listener.onCardItemClick(model));
    }

    public void setOnItemCardClickListener(Listener listener){
        this.listener = listener;
    }
    public interface Listener{
        void onCardItemClick(Request request);
    }

    public class NotificationViewHolder extends RecyclerView.ViewHolder {
        SingleNotificationItemBinding binding;

        public NotificationViewHolder(@NonNull SingleNotificationItemBinding itemView) {
            super(itemView.notifyCardItem);
            this.binding = itemView;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚删除了setHasFixedSize(true),它有所帮助!