我想要做的是在回收者查看器中放置联系人列表。联系人列表应包含联系人图像,联系电话和联系人姓名。我已经解决了三个中的后者,但问题是图像没有分配给联系人姓名。基本上我希望每个联系人都可以从联系人列表或Facebook分配图像。任何人都可以试着看看我哪里出错了。提前谢谢。
public class allContacts extends Fragment {
RecyclerView r;
ContactAdapter adapter;
List <contactInfo> contactLists = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_contacts, container, false);
r =(RecyclerView)view.findViewById(R.id.contactView);
loadContact contact = new loadContact();
contact.execute();
return view;
}
public class loadContact extends AsyncTask<Void, Void, ArrayList<contactInfo>> {
ProgressDialog pd;
ArrayList<contactInfo> contacts;
contactInfo contact;
String imnage;
Cursor phonesCursor;
Bitmap contactPhoto;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(getActivity(), "Loading Contacts", "Please Wait");
}
@Override
protected ArrayList<contactInfo> doInBackground(Void... params) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(Contacts.CONTENT_URI, null, null, null, CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
contact = new contactInfo();
contact.setName(name);
Cursor phoneCursor = contentResolver.query(
CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(CommonDataKinds.Phone.NUMBER));
contact.setPhoneNumber(phoneNumber);
String contactId = cursor.getString(cursor.getColumnIndex(Contacts._ID));
Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId));
InputStream input = Contacts.openContactPhotoInputStream(contentResolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
CommonDataKinds.Email.CONTENT_URI,
null,
CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(CommonDataKinds.Email.DATA));
}
contactLists.add(contact);
}
}
return null;
}
@Override
protected void onPostExecute(ArrayList<contactInfo> contacts) {
super.onPostExecute(contacts);
pd.cancel();
ContactAdapter contactAdapter = new ContactAdapter(contactLists, getActivity().getApplicationContext());
r.setLayoutManager(new LinearLayoutManager(getActivity()));
r.setAdapter(contactAdapter);
}
}
public class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder> {
private List<contactInfo> contactLists;
private Context context;
Cursor cursor;
public ContactAdapter(List <contactInfo> contactLists , Context context)
this.contactLists = contactLists;
this.context = context;
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.contactviewer,null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
contactInfo contact = contactLists.get(position);
holder.contactName.setText(contact.getName());
holder.contactNumber.setText(contact.getPhoneNumber());
loadContact l = new loadContact();
holder.contactPic.setImageBitmap(l.contactPhoto);
}
@Override
public int getItemCount() {
return contactLists.size();
}
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
ImageView contactPic;
TextView contactName;
TextView contactNumber;
public ContactViewHolder(View itemView) {
super(itemView);
contactPic = (ImageView) itemView.findViewById(R.id.imageView101);
contactName = (TextView) itemView.findViewById(R.id.Text101);
contactNumber = (TextView) itemView.findViewById(R.id.Text202);
}
}
}
class contactInfo{
String phoneNumber;
String Name;
int image;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public contactInfo(){
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
答案 0 :(得分:0)
在你的onBindViewHolder方法中,你试图像这样再次设置相同的图像
loadContact l = new loadContact();
holder.contactPic.setImageBitmap(l.contactPhoto);
而是尝试设置
holder.contactPic.setImageBitmap(contact.getImage());