我正在尝试构建一个android应用程序,在其中导入手机中的所有联系人,然后检查手机号码是否存在于用户节点中。如果否,则itemview的可见性将消失,如果是,则可见性将可见。但是,一旦它是可见的,我想获得该特定用户的id,以便我可以执行一些特定的任务。
contactsadapter.java
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
Activity activity;
ArrayList<Contacts> arrayList;
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users");
public ContactsAdapter(Activity activity, ArrayList<Contacts> arrayList){
this.activity = activity;
this.arrayList = arrayList;
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {
Contacts model = arrayList.get(position);
String phoneNo = model.getNumber();
phoneNo = phoneNo.replaceAll("\\s", "");
holder.show_name.setText(model.getName());
holder.show_message.setText(phoneNo);
holder.notific.setVisibility(View.GONE);
holder.accReq.setVisibility(View.GONE);
holder.sendReq.setVisibility(View.GONE);
holder.timeLast.setVisibility(View.GONE);
holder.img_on.setVisibility(View.GONE);
Query query = userRef.orderByChild("phno").equalTo(phoneNo);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (!snapshot.exists()){
holder.itemView.getLayoutParams().height = 0;
holder.itemView.setVisibility(View.GONE);
}else {
// want to get the userID to perform some click events.
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message, show_name, timeLast;
public ImageView personAvatarR, sendReq;
public CircleImageView img_on,notific;
public Button accReq;
public ViewHolder(@NonNull View itemView) {
super(itemView);
accReq = itemView.findViewById(R.id.accReq);
show_message = itemView.findViewById(R.id.personHolderEmail);
show_name = itemView.findViewById(R.id.personHolderName);
personAvatarR = itemView.findViewById(R.id.personAvatar);
timeLast = itemView.findViewById(R.id.timeLast);
img_on = itemView.findViewById(R.id.img_on);
notific = itemView.findViewById(R.id.notific);
sendReq = itemView.findViewById(R.id.sendReq);
}
}
}
contactactivity中的getcontactlist()方法
private void getContactList() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String sort = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC";
Cursor cursor = getContentResolver().query(
uri,null,null,null,sort
);
if (cursor.getCount()>0){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Uri uriPhone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" =?";
Cursor phoneCursor = getContentResolver().query(
uriPhone,null,selection,new String[]{id}, null
);
if (phoneCursor.moveToNext()){
String number = phoneCursor.getString(phoneCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
));
Contacts model = new Contacts();
model.setName(name);
model.setNumber(number);
arrayList.add(model);
phoneCursor.close();
}
}
cursor.close();
}
contactList.setLayoutManager(new LinearLayoutManager(this));
adapter = new ContactsAdapter(this, arrayList);
contactList.setAdapter(adapter);
}
暂无答案!
目前还没有任何答案,快来回答吧!