选择联系人并将其显示在recyclerview中

时间:2018-09-18 11:55:35

标签: java android xml android-studio android-recyclerview

SettingsContacts

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

AdapterContacts

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

}

对不起,但是我把它弄糟了……就像我找到了提取所有联系人的代码一样……但是我不知道如何实现它并在recyclerview中显示它……这是我的新知识。任何人都可以帮助我吗...预先感谢...另外,如果您需要xml布局的代码,请询问...

3 个答案:

答案 0 :(得分:0)

尝试一下:

@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<>();

答案 1 :(得分:0)

尝试在mAdapter.notifyDataSetChanged();循环后添加while

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

编辑:

定义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();

答案 2 :(得分:0)

您要向适配器添加一个空列表:

android:maxLines="1"
android:maxWidth="160dp"

这样做:

mAdapter = new AdapterContacts(contacts);