从Firebase数据库和电话簿中读取常用联系人 [英] Reading common contacts from firebase database and phonebook

查看:45
本文介绍了从Firebase数据库和电话簿中读取常用联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase数据库结构

我正在构建一个跟踪应用程序,用户可以从电话簿中选择关注者,然后可以跟踪他们的旅程.因此,要实现此目的,我(用户)必须访问其电话簿并选择要发送的联系人关注请求.为此,我希望我的应用程序仅显示安装了该应用程序的联系人.我创建了一个用户类来将用户的详细信息存储在firebase数据库中.我可以从我的电话簿中访问联系人.另外,我能够在Firebase数据库中存储正在使用我的应用程序的人员的详细信息.当我尝试从Firebase数据库检索用户的电话号码时,它没有给我预期的结果.有人可以在这方面帮助我吗?

I am building a tracking application where a user can select a follower from their phonebook who can then track their journey.So to implement this,I(user) has to access their phonebook and select the contacts who they would like to send a follow request. For this, I want my application to show only the contacts who have the app installed. I have created a user class to store the details of the users in the firebase database.I am able to access the contacts from my phonebook. Also,I am able to store the details of the people who are using my application in firebase database. When I am trying to retrieve the phone numbers of the users from the firebase database, it does not give me the result as expected. Can someone help me in this regard?

我创建了一个Main活动,其中包含Firebase数据库.当新用户注册时,其详细信息将输入到Firebase数据库中.我的firebase的结构如下所示: Contactsfirebase-16ade

I have created a Main activity which has Firebase database in it. When a new user registers their details are fed into the Firebase database. The structure of my firebase looks like this: contactsfirebase-16ade

 users
   -LiNCTO6eR2TVmKmJXLE
      email: "kira_6809@yahoo.com"
       name: "kiran"
      phone: "1234567890"

在下一个活动中,为进行试用,我添加了一个按钮和一个ListView,以便当我单击该按钮时,我应该能够看到firebase数据库中存在的用户的所有详细信息.我用于所有这些的代码.

In the next activity, for trial I have added a button and a ListView such that when I click on the button,I should be able to see all the details of the users present in the firebase database.I have shared below the code that I am using for all of this.

MainActivity:

MainActivity:

EditText Name,Email,Phone;
Button Submit,Load;
DatabaseReference databaseReference;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Name= findViewById(R.id.Name);
 Email= findViewById(R.id.Email);
 Phone= findViewById(R.id.Phone);
 Submit= findViewById(R.id.Submit);
 Load= findViewById(R.id.Next);
 databaseReference=
 FirebaseDatabase.getInstance().getReference("users");

Submit.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    AddUser();
  }
});

Load.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    startActivity(new Intent(MainActivity.this,ShowFirebase.class));
  }
});
}
private void AddUser() {
String name = Name.getText().toString().trim();
String email = Email.getText().toString().trim();
String phone = Phone.getText().toString();
User user = new User(name,email,phone);
String id = databaseReference.push().getKey();
if((!name.isEmpty())&&(!email.isEmpty())&&(!phone.isEmpty())){
  databaseReference.child(id).setValue(user);
  Toast.makeText(this,"Registered successfully",Toast.LENGTH_LONG).show();
  startActivity(new Intent(MainActivity.this,ShowFirebase.class));


}else{
  Toast.makeText(this,"Retry",Toast.LENGTH_LONG).show();
}
}
}

ShowFirebaseActivity:

ShowFirebaseActivity:

public class ShowFirebase extends AppCompatActivity {
ListView ListView;
Button Load;
DatabaseReference databaseReference;


@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_firebase);
    ListView = findViewById(R.id.ListView);
    Load = findViewById(R.id.Next);
    databaseReference = 
    FirebaseDatabase.getInstance().getReference();
    DatabaseReference phoneref = databaseReference.child("phone");

    //String id = databaseReference.getKey();


    Load.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            databaseReference.child("users");
            databaseReference.child("id");
            databaseReference.child("phone");
            databaseReference.addValueEventListener(new 
ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    ArrayList<String> numbers = new ArrayList<>();
                    for (DataSnapshot ds : 
dataSnapshot.getChildren()) {
                        String number = ds.getKey();
                        numbers.add(number);
                    }
                    ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,numbers);
                    ListView.setAdapter(stringArrayAdapter);


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


        }
    });

}

}

用户类别:

public class User {
String Name,Email,Phone;

public User(String name, String email, String phone) {
Name = name;
Email = email;
Phone = phone;
}

public String getName() {
return Name;
}

public String getEmail() {
return Email;
}

public String getPhone() {
return Phone;
}
}

这样的代码没有错误,但是当我们单击ShowFirebase活动中的按钮时,唯一显示的是文本用户".我希望看到Firebase数据库中显示的所有电话号码.

There is no error in the code as such, but when we are clicking the button in the ShowFirebase Activity, the only thing that is showing up is the text "users". I expect to see all the phone numbers present in the firebase database.

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);
    }

}

}

推荐答案

更改此内容:

            databaseReference.child("users");
            databaseReference.child("id");
            databaseReference.child("phone");
            databaseReference.addValueEventListener(new 
ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    ArrayList<String> numbers = new ArrayList<>();
                    for (DataSnapshot ds : 
dataSnapshot.getChildren()) {
                        String number = ds.getKey();
                        numbers.add(number);
                    }
                    ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,numbers);
                    ListView.setAdapter(stringArrayAdapter);


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

对此:

            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);
                        numbers.add(number);
                    }
                    ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,numbers);
                    ListView.setAdapter(stringArrayAdapter);


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

添加对节点users的引用,然后在userid中进行迭代并获取phone值.

Add a reference to node users then iterate inside the userid and retrieve the phone value.

这篇关于从Firebase数据库和电话簿中读取常用联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆