Выбор контактов и отображение их в утилите - PullRequest
0 голосов
/ 18 сентября 2018

НастройкиКонтакты

public class SettingsContacts extends AppCompatActivity {
private RecyclerView contactsList;
private List<ContactsHelper> contacts = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private AdapterContacts mAdapter;


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

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    //Add the data first
    addDataToList();
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    //and then create a object and pass the lis
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();


}

public void addDataToList(){
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                            null);
                    if (phoneCursor != null) {
                        if (phoneCursor.moveToNext()) {
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            contacts.add(new ContactsHelper(name, phoneNumber));
                            phoneCursor.close();
                        }
                    }
                }
            }
        }
        cursor.close();
    }
}
}

АдаптерКонтакты

ppublic class AdapterContacts extends RecyclerView.Adapter<AdapterContacts.ContactViewHolder>{

private List<ContactsHelper> mContacts;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;

public AdapterContacts(List<ContactsHelper>mContacts)

{
    this.mContacts = mContacts;
}

public AdapterContacts(String name, String phoneNumber) {
}

public class ContactViewHolder extends RecyclerView.ViewHolder{
    public TextView nameText;
    public TextView phonenumberText;
    public ContactViewHolder(View view)
    {
        super(view);
        nameText = (TextView)view.findViewById(R.id.contact_text_layout);
        phonenumberText = (TextView)view.findViewById(R.id.contact_text_layout2);
    }
}
@Override
public AdapterContacts.ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_contact,parent,false);
    mAuth = FirebaseAuth.getInstance();
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();
    return new AdapterContacts.ContactViewHolder(V);


}

@Override
public void onBindViewHolder(final AdapterContacts.ContactViewHolder holder, int position) {


    ContactsHelper contacts = mContacts.get(position);
    String name = contacts.getName();
    String phoneNumber = contacts.getPhoneNumber();
    holder.nameText.setText(name);
    holder.phonenumberText.setText(phoneNumber);

}

@Override
public  int getItemCount() {
    return mContacts.size();
}

}

ContactsHelper

public class ContactsHelper {
private String Name;
private String PhoneNumber;

public ContactsHelper() {
}

public ContactsHelper(String Name, String PhoneNumber) {
    this.Name = Name;
    this.PhoneNumber = PhoneNumber;
}
public String getName() {
    return Name;
}

public void setName(String Name) {
    this.Name = Name;
}

public String getPhoneNumber() {
    return PhoneNumber;
}

public void setPhoneNumber(String PhoneNumber) {
    this.PhoneNumber = PhoneNumber;
}

}

Извините, ноЯ испортил это ... Как я нашел код для извлечения всех контактов ... Но я понятия не имею, как реализовать это и отобразить это в представлении переработчика ... Я новичок в этом, так что любой может помочь мне, пожалуйста, выручить меня... Заранее спасибо ... Также, если вам нужен код макетов XML, пожалуйста, спросите ...

Ответы [ 3 ]

0 голосов
/ 18 сентября 2018

Попробуйте добавить mAdapter.notifyDataSetChanged(); после while loop

while(cursor.moveToNext()) {
    .
    .
    .
}
mAdapter.notifyDataSetChanged();

EDIT:

Определите contacts перед передачей их в mAdapter и переместите addDataToList() в нижнюю часть.

contactsList = (RecyclerView) findViewById(R.id.usersList);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
//and then create a object and pass the lis

final ArrayList<Contacts> contacts = new ArrayList<>();
mAdapter = new AdapterContacts(contacts);

contactsList.setHasFixedSize(true);
contactsList.setLayoutManager(linearLayoutManager);
contactsList.setAdapter(mAdapter);
addDataToList();
0 голосов
/ 18 сентября 2018

Вы добавляете в адаптер пустой список:

mAdapter = new AdapterContacts(contacts);

Сделайте это:

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

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());

    //HERE add the data in the contacts array before add it into the adapter
    contacts = getContacts();
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
}


private ArrayList<Contacts> getContacts(){
    ArrayList<Contacts> contactList = new ArrayList<Contacts>();
    ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                    if (hasPhoneNumber > 0) {
                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Cursor phoneCursor = contentResolver.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                                null);
                        if (phoneCursor != null) {
                            if (phoneCursor.moveToNext()) {
                                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                contactList.add(new Contacts(name, phoneNumber));
                                phoneCursor.close();
                            }
                        }
                    }
                }
            }
            cursor.close();
        }
return contactList;

}
0 голосов
/ 18 сентября 2018

Попробуйте это:

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

    contactsList = (RecyclerView) findViewById(R.id.usersList);
    //Add the data first 
    addDataToList();
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    //and then create a object and pass the lis
    mAdapter = new AdapterContacts(contacts);

    contactsList.setHasFixedSize(true);
    contactsList.setLayoutManager(linearLayoutManager);
    contactsList.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();


}
public void addDataToList(){
final ArrayList<Contacts> contacts = new ArrayList<>();
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                            null);
                    if (phoneCursor != null) {
                        if (phoneCursor.moveToNext()) {
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            contacts.add(new Contacts(name, phoneNumber));
                            phoneCursor.close();
                        }
                    }
                }
            }
        }
        cursor.close();
    }
}
}

Объявить это глобально

 ArrayList<Contacts> contacts = new ArrayList<>();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...