1

I have used the following dependencies firestore:12.0.1 firebase-ui-firestore:3.3.0

The code compiles without any errors and app runs just fine but the recyclerview is not populated with data. I have used similar code before numerous times but never encountered any problem till now. could firebase-ui the cause?Any help is appreciated.

public class ChatActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView messageList;

    FirebaseFirestore fireShop = FirebaseFirestore.getInstance();
    private FirestoreRecyclerAdapter firestoreRecyclerAdapter;
    private FirebaseAuth firebaseAuth;
    private ImageButton imageButton;
    private EditText editText;

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

        firebaseAuth = FirebaseAuth.getInstance();
        imageButton = (ImageButton) findViewById(R.id.button_chatbox_send);
        editText = (EditText) findViewById(R.id.edittext_chatbox) ;
        imageButton.setOnClickListener(this);


            messageList = (RecyclerView) findViewById(R.id.reyclerview_message_list);
            messageList.setHasFixedSize(true);
            messageList.setLayoutManager(new LinearLayoutManager(this));
            fetchData();




    }
    public class MessageViewHolder extends RecyclerView.ViewHolder{
        View mView;

        public MessageViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
        }
        public void setMess(String messs){

            TextView messageView = (TextView) mView.findViewById(R.id.text_message_body);
            messageView.setText(messs);
        }

    }


    @Override
    public void onClick(View v) {
        if (v == imageButton){
            addData();

        }
    }
    public void fetchData( ){


        Query query=fireShop.collection("chat");
        Log.d("SGGGGG",query.toString());

        FirestoreRecyclerOptions<MessageDetails> details = new FirestoreRecyclerOptions.Builder<MessageDetails>()
                .setQuery(query,MessageDetails.class)
                .build();
        Log.d("ffffffffffFFRRRRRRRRRG",details.getSnapshots().toString());
        firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<MessageDetails, MessageViewHolder>(details) {

            @Override
            protected void onBindViewHolder(@NonNull MessageViewHolder holder, int position, @NonNull MessageDetails model) {
                Log.d("FFFFFFFFFFFRRRRRRRRRG",model.getMessage());

                holder.setMess(model.getMessage());


            }

            @Override
            public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_message_sent,parent,false);
                return new MessageViewHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) {
                Log.e("error", e.getMessage());
            }


        };
        messageList.setAdapter(firestoreRecyclerAdapter);

        firestoreRecyclerAdapter.notifyDataSetChanged();




    }

   @Override
    public void onStop() {
        super.onStop();
        firestoreRecyclerAdapter.stopListening();
    }
    @Override
    public void onStart() {
        super.onStart();
        firestoreRecyclerAdapter.startListening();
    }



}

Data Screenshot

Security rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}
Atul
  • 11
  • 1
  • 3

2 Answers2

0

If you are not using any sign-in methods like any of these then you must use firestore in test mode, if you use Firestore in lock mode then your users must be authenticated with sign-in methods (as in above link) to access data from firestore.

Just change your security rules to

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

and try again.

Mohammed Farhan
  • 1,120
  • 8
  • 14
0

You need to create an empty constructor for MessageDetails.class

Just had a similar problem and resolved it by creating an empty constructor for the class of the main object.

Temur
  • 41
  • 4