选择联系人并在recyclerview中显示它

问题描述 投票:0回答:3

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布局的代码,请问...

java android xml android-studio android-recyclerview
3个回答
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<>();

0
投票

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

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
投票

尝试在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(); 移动到底部。

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.