我想将Firebase数据库中存在的电话号码与手机中通讯录中的电话号码进行比较,以便可以在应用程序中显示所有常用电话号码。链接中提到了该问题的完整说明:
Reading common contacts from firebase database and phonebook
我已将Firebase电话号码加载到一个ArrayList中,并将联系人中的电话号码加载到另一个ArrayList中。然后在两个ArrayLists上实现retainsall()方法以查找公共数据。
代码:
public class ShowFirebase extends AppCompatActivity {
ListView ListView;
Button Load;
String phoneNumber;
String name;
DatabaseReference databaseReference;
ArrayList<String> aa = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_firebase);
GetNumber(this.getContentResolver());
ListView = findViewById(R.id.ListView);
Load = findViewById(R.id.Next);
databaseReference =
FirebaseDatabase.getInstance().getReference();
databaseReference =
FirebaseDatabase.getInstance().getReference().child("users");
databaseReference.addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot
dataSnapshot) {
ArrayList<String> numbers = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String number =
ds.child("phone").getValue(String.class);
String name =
ds.child("name").getValue(String.class);
numbers.add(number);
}
numbers.retainAll(aa);
ArrayAdapter stringArrayAdapter = new ArrayAdapter<>
(getApplicationContext(), android.R.layout.simple_list_item_1,
numbers);
ListView.setAdapter(stringArrayAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError
databaseError) {
}
});
}
private void GetNumber(ContentResolver contentResolver) {
Cursor phones =
contentResolver.query(ContactsContract.CommonDataKinds.Phone.
CONTENT_URI, null, null, null,
ContactsContract.Contacts.SORT_KEY_PRIMARY + "
ASC");
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("......." + phoneNumber);
aa.add(name + " " + phoneNumber);
}
}
}
我希望具有在电话簿中保存的姓名的常用电话号码将显示在ListView中。但是Listview没有给出任何输出