我正在显示一些联系人列表。我也想展示联系人的照片。这些我想直接从联系人加载并在列表中显示它们。我应该在适配器中执行此操作吗?我怎么能这样做?
我在列表中只添加了选定的联系人。所以我必须只显示所选联系人的照片。
我试过这样:
public class InviteeAdapter extends RecyclerView.Adapter<InviteeAdapter.MyViewHolder>{
private List<Invitee> inviteeList;
int status;
Context context;
Cursor mCursor;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView profileImage;
private String imageUri;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.scheduleName);
profileImage = (ImageView) view.findViewById(R.id.eventsIcon);
}
}
public InviteeAdapter(List<Invitee> inviteeList,Context context) {
this.inviteeList = inviteeList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.invitee_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Invitee invitee = inviteeList.get(position);
holder.name.setText(invitee.getFName()+" "+invitee.getLName());
// String imageUri = invitee.getImageUri();
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, "upper("+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
while (mCursor.moveToNext()) {
holder.imageUri = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
// photo.setImageUri(image_uri);
}
if(holder.imageUri!=null) {
holder.profileImage.setImageURI(Uri.parse(holder.imageUri));
}
/* status = invitee.;
if(status == 0)
{
holder.name.setTextColor(context.getResources().getColor(R.color.colorAccent));
}
else {
holder.name.setTextColor(context.getResources().getColor(R.color.grey));
}*/
}
@Override
public int getItemCount() {
return inviteeList.size();
}
}
但是,managedquery显示未解决。这该怎么做?请帮忙。谢谢。
编辑:我有一个Invitee类和一个Invitee活动,我在其中获取联系人列表及其数据,我正在使用复选框选择联系人,此列表存储在选定的联系人中,并将其添加到服务器中&# 39; s数据库。我有一个AsyncTask
到addInvitee
以及getInvitee
。
所以在InviteeActivity
我从这里调用getInvitee
asyncTask我得到了一系列受邀联系人。现在,当我在InviteeActivity
中显示此选定的联系人列表时,我想显示联系人的图像。
AddInvitee
活动:
public class AddInviteeActivity extends AppCompatActivity {
private ArrayList<Invitee> contact_list = null;
private inviteeAdapter mInviteeAdapter;
private String contactId,display_name,email,phone,image_uri;
private ArrayList<Invitee> items;
private ArrayList<Invitee> selectedContacts;
boolean[] isChecked;
Cursor mCursor;
public ListView lv;
public int RQS_PICK_CONTACT = 1,RQS_ADD_CONTACT = 2;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
ArrayList<Integer> selectedItemsPositions;
private ImageView done,back;
private boolean mContactListActivity;
private FloatingActionButton fab;
public Invitee invitee;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_invitee);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setText("Select Contacts");
setSupportActionBar(toolbar);
done = (ImageView)findViewById(R.id.done);
back = (ImageView)findViewById(R.id.back);
fab = (FloatingActionButton)findViewById(R.id.fab);
contact_list = new ArrayList<Invitee>();
selectedContacts = new ArrayList<Invitee>();
lv = (ListView)findViewById(R.id.list);
showContacts();
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
invitee = new Invitee();
Log.d("selectd",String.valueOf(selectedItemsPositions));
mContactListActivity = true;
selectedContacts = new ArrayList<>();//to store selected items
for (Integer pos : selectedItemsPositions) {
selectedContacts.add(items.get(pos));
}
if(selectedContacts.size() == 0) {
Toast.makeText(AddInviteeActivity.this,"Please Select Contact.",Toast.LENGTH_SHORT).show();
}
else {
Invitations invitations = new Invitations(1,selectedContacts);
new AddInviteeAsyncTask(AddInviteeActivity.this).execute(invitations);
finish();
}
}
});
}
@SuppressWarnings("unused")
private void getContacts() {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, "upper("+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
while (mCursor.moveToNext()) {
invitee = new Invitee();
Invitee photo = new Invitee();
contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
display_name = mCursor.getColumnName(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
invitee.setFName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
invitee.setEmail(getEmail(contactId));
invitee.setMobile(getPhoneNumber(contactId));
contact_list.add(invitee);
}
isChecked = new boolean[mCursor.getCount()];
for (int i = 0; i < isChecked.length; i++) {
isChecked[i] = false;
}
this.mInviteeAdapter = new inviteeAdapter(AddInviteeActivity.this, R.layout.contact_list_item, contact_list);
lv.setAdapter(this.mInviteeAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RQS_PICK_CONTACT) {
if (resultCode == RESULT_OK) {
getContacts();
}
}
}
public class inviteeAdapter extends ArrayAdapter<Invitee> {
public inviteeAdapter(Context context, int textViewResourceId, ArrayList<Invitee> items1) {
super(context, textViewResourceId, items1);
items = items1;
selectedItemsPositions = new ArrayList<>();
}
//to store all selected items position
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mViewHolder;
if (convertView == null) {
mViewHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.contact_list_item, parent, false);
mViewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
mViewHolder.name = (TextView) convertView.findViewById(R.id.title);
mViewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
int position = (int) mViewHolder.cb.getTag();
if (ischecked) {
//check whether its already selected or not
if (!selectedItemsPositions.contains(position))
selectedItemsPositions.add(position);
} else {
//remove position if unchecked checked item
selectedItemsPositions.remove((Object) position);
}
}
});
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
Invitee invitee = items.get(position);
mViewHolder.cb.setTag(position);
if (selectedItemsPositions.contains(position))
mViewHolder.cb.setChecked(true);
else
mViewHolder.cb.setChecked(false);
mViewHolder.name.setText(invitee.getFName());
return convertView;
}
public class ViewHolder {
CheckBox cb;
TextView name;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
getContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we can not display the names", Toast.LENGTH_SHORT).show();
}
}
}
private void showContacts()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
}
else {
getContacts();
}
}
@Override
public void onResume() {
super.onResume();
// mContactAdapter.notifyDataSetInvalidated();
//this.recreate();
if(mInviteeAdapter != null) {
mInviteeAdapter.clear();
getContacts();
}
// mContactAdapter.notifyDataSetChanged();
}
private String getPhoneNumber(String contactId) {
String phoneNumber = "";
final String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, };
final Cursor phone = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
while (!phone.isAfterLast()) {
phoneNumber = phoneNumber + phone.getString(contactNumberColumnIndex) + ";";
phone.moveToNext();
}
}
phone.close();
return phoneNumber;
}
private String getEmail(String contactId) {
String emailStr = "";
final String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA, // use
// Email.ADDRESS
// for API-Level
// 11+
ContactsContract.CommonDataKinds.Email.TYPE };
final Cursor email = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (email.moveToFirst()) {
final int contactEmailColumnIndex = email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!email.isAfterLast()) {
emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
email.moveToNext();
}
}
email.close();
return emailStr;
}
public String getLastName()
{
String lastName = "";
Cursor nameCur = AddInviteeActivity.this.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null,null,null,ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
lastName = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();
return lastName;
}
public String getPhoto(String contactId)
{
String imageUri ="";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
imageUri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
}
}
return imageUri;
}
}
InviteeActivity:
public class InviteeActivity extends AppCompatActivity implements GetInviteeAsyncTask.GetInvitees{
private RecyclerView recyclerView;
private InviteeAdapter mAdapter;
private ArrayList<Invitee> inviteeList = new ArrayList<>();
private FloatingActionButton fab;
private TextView alertText;
private RelativeLayout content;
private boolean mInvitee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invitee);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Invitee");
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
new GetInviteeAsyncTask(this,InviteeActivity.this,inviteeList).execute(1L);
alertText = (TextView)findViewById(R.id.alertText);
content = (RelativeLayout)findViewById(R.id.content);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mInvitee = true;
Intent i = new Intent(InviteeActivity.this,AddInviteeActivity.class);
i.putExtra("Invitee",mInvitee);
startActivity(i);
}
});
}
@Override
public void doPostExecute(ArrayList<Invitee> response) {
inviteeList = response;
if(inviteeList.size() > 0)
{
alertText.setVisibility(View.INVISIBLE);
content.setVisibility(View.VISIBLE);
}
else {
alertText.setVisibility(View.VISIBLE);
content.setVisibility(View.INVISIBLE);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new InviteeAdapter(inviteeList,InviteeActivity.this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
}
答案 0 :(得分:0)
从URL获取图像必须在异步任务中完成,因此您应该创建Async类,如下所示:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
this.bmImage.setImageBitmap(result);
}
}
适配器中的更改此行:
if(holder.imageUri!=null) {
holder.profileImage.setImageURI(Uri.parse(holder.imageUri));
}
<强> TO: 强>
if(holder.imageUri!=null) {
new DownloadImageTask(holder.profileImage ).execute(Uri.parse(holder.imageUri));
}