Reputation: 316
I am trying to see all those Contacts which I have on my phone and have registered on the app. I am able to see the registered contact, but as you can see, I have a single entry in the database by username John and in the Android app, it is showing more than one. I only want to show it once. I don't know the reason, Can anybody Help? Here is the Code
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter mUserlist;
ArrayList<ProfileInfo> list, userList;
DatabaseReference db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
db = FirebaseDatabase.getInstance().getReference().child("UserInfo");
recyclerView = view.findViewById(R.id.UserRecyclerView);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
list = new ArrayList<>();
userList = new ArrayList<>();
mUserlist = new UsersAdapter(userList);
recyclerView.setAdapter(mUserlist);
displayContactList();
return view;
}
private void displayContactList() {
Cursor numberList = getActivity().getApplicationContext().getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
assert numberList != null;
while (numberList.moveToNext()) {
String name = numberList.getString(numberList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = numberList.getString(numberList.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ProfileInfo info = new ProfileInfo(name, phoneNumber);
list.add(info);
showAppUsers(info);
}
}
private void showAppUsers(ProfileInfo info){
Query query = db.orderByChild("phoneNumber").equalTo(info.getPhoneNumber());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() ){
String phone ="",
name="";
for (DataSnapshot child : dataSnapshot.getChildren()){
if (child.child("phoneNumber").getValue() != null)
name = Objects.requireNonNull(child.child("userName").getValue()).toString();
if (child.child("userName").getValue() != null)
phone = Objects.requireNonNull(child.child("phoneNumber").getValue()).toString();
ProfileInfo info = new ProfileInfo(name, phone);
userList.add(info);
mUserlist.notifyDataSetChanged();
return;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Upvotes: 1
Views: 115
Reputation: 138824
As I see in your database, you only have a single record. When using the following query:
Query query = db.orderByChild("phoneNumber").equalTo(info.getPhoneNumber());
Indeed a single record will be returned. The problem is not your query. The problem lies in the fact that you are calling showAppUsers()
method from within a while loop. Most probably, your numberList
contains three records, hence that behavior. So at every iteration you add to the userList
the value of the userName
property. To solve this, get the call to showAppUsers()
method out of the loop so you can get only one result.
Upvotes: 1