Stark Stark - 6 months ago 71
Android Question

Firebase is returning 3 times the same value

I'm having some problems after the Firebase made its last update. My biggest problem at the moment is that I am unable to fix a class of application that I had done before this update.
The class below is returning 3 times the same name in my listview, and I have 3 registered users and all 3 have different names.

Note: It returns 3 times the name of the User that the application are logged.

package com.example.stark.chattest;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.stark.chattest.model.Message;
import com.example.stark.chattest.model.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;

public class ChatActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference("users");
private String TAG = "Resultado: ";
private ArrayList<String> userName = new ArrayList<String>();
private ListView usersListView = (ListView) findViewById(R.id.chatListView);
private ListAdapter usersListViewAdapter = new ArrayAdapter<String>(ChatActivity.this, android.R.layout.simple_list_item_1, userName);

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

mAuth = FirebaseAuth.getInstance();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
} else {
Intent intent = new Intent(ChatActivity.this, SignActivity.class);
startActivity(intent);
finish();
}
}
};

ImageButton sendImageButton = (ImageButton) findViewById(R.id.sendImageButton);

sendImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText messageEditText = (EditText)findViewById(R.id.messageEditText);
FirebaseUser firebaseUser = mAuth.getCurrentUser();
Message message = new Message();
message.setMessage(String.valueOf(messageEditText.getText()));
message.setRead(true);
message.setTime(22221);
message.setSendersID(firebaseUser.getUid());
message.setReceiversID("WTImZyGv5fcOJlGWI9fi0gdrfbd2");

myRef.child(message.getSendersID()).child("message").child(message.getReceiversID()).child("4").setValue(message);

//Set the Reciever Message
message.setRead(false);
myRef.child(message.getReceiversID()).child("message").child(message.getSendersID()).child("4").setValue(message);
}
});

ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
User user = dataSnapshot.getValue(User.class);
userName.add(user.getName());
usersListView.setAdapter(usersListViewAdapter);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
myRef.addChildEventListener(childEventListener);
}
}


Here is my DB table.

Note: English is not my mother language :p

@Edit
I removed everything within the onChildChanged and put all the code in onChildAdded.

Answer

You should change a couple of things in your code, for e.g this line

ListView usersListView = (ListView) findViewById(R.id.chatListView);
ListAdapter usersListViewAdapter = new ArrayAdapter<String>(ChatActivity.this, android.R.layout.simple_list_item_1, userName);

write this line in your onCreate() method, you need to initialize only once,

and regarding querying users,you should do this in your onChildAdded() method instead of onChildChanged(), and you will not have to parse through the results with a for loop it will be automatically added

A little code to explain what i said,

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
    User user = dataSnapshot.getValue(User.class);
    userName.add(user.getName());
    usersListViewAdapter.notifyDataSetChanged();
}

you can remove code from onChildChanged(), See if this helps