• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have a code down there. I list the phone numbers in the directory with ArrayList and send it to the firebase database. I need to create a separate key for each phone number and name. But I failed. I write a database with the code as follows.

{
  "ContactPhoneNumbers" : {
    "-LcxMT5-D4KvWi3ZbFyi" : {
      "-LcxMT5-D4KvWi3ZbFyi" : {
        "name" : [ "ambulance", "police" ],
        "phone" : [ "112", "155" ]
      }
    }
  }
}

I will use this database later in an application. But it shouldn't be like this. I'm writing the database I want to create manually. It has to be like this.

{
  "ContactPhoneNumbers" : {
    "-LcaH_gtgarJwbY5-C08" : {
      "LcaH_gtgarAU54FU-C08" : {
        "name" : "police",
        "phone" : 155
      },
      "LcaH_gtgayuUU154TY" : {
        "name" : "ambulance",
        "phone" : 112
      }
    }
  }
}

I want to create a custom key for each phone number and name. Can I do this with my code below? I'd appreciate it if you could help.

Here is the UserPermissionActivity.

public class UserPermissionActivity extends AppCompatActivity {

    Cursor cursor;
    ArrayList<String> contacts,contacts2;

    private DatabaseReference mDatabase;

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

        FirebaseApp.initializeApp(this);
        mDatabase = FirebaseDatabase.getInstance().getReference();

        saveContacts();
     }

    private void saveContacts() {

        final String key =  mDatabase.push().getKey();

        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");

        contacts = new ArrayList<String>();
        contacts2 = new ArrayList<String>();

        while (cursor.moveToNext()){
            contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contacts.add(contactName);
            contacts2.add(phoneNumber);

            mDatabase.child("ContactPhoneNumbers").child(key).child(key).child("name").setValue(contacts);
            mDatabase.child("ContactPhoneNumbers").child(key).child(key).child("phone").setValue(contacts2);
        }

        cursor.close();
    }
}

You need to have a class like the following.

public class User {

    public String name;
    public String phone;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }
}

Now you may consider declaring a function like the following.

private void writeNewUser(String name, String phone) {
    final String key =  mDatabase.push().getKey();
    User user = new User(name, phone);
    mDatabase.child("ContactPhoneNumbers").child(key).setValue(user);
}

Then call this function while you are saving your contacts.

private void saveContacts() {
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,
    ContactsContract.Contacts.DISPLAY_NAME + " ASC");

    while (cursor.moveToNext()){
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        // Call the function here
        writeNewUser(contactName, phoneNumber);
    }

    cursor.close();
}

Hope that solves your problem.

  • 1
Reply Report