我正在将应用程序的所有用户都放在一个列表中,但我只想显示现有电话联系人的用户;所以我在列表中有所有电话号码,我想查询它们是否存在于联系人列表中,但我找不到一种方法。我想让它像Whatsapp添加消息系统一样工作。谁能帮我?谢谢!
void GetPeoples(){
Users users = new Users();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Users, UsersViewHolder>
(
Users.class,
R.layout.users_display_layout,
UsersViewHolder.class,
allDatabaseUserreference.orderByChild("phone_number").equalTo(numbers.size())
) {
@Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, final int position) {
//if(numbers.contains(model.getPhone_number())){
viewHolder.setUser_name(model.getUser_name());
viewHolder.setUser_status(model.getUser_status());
viewHolder.setUser_thumb_image(getApplicationContext(), model.getUser_thumb_image());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String visit_user_id = getRef(position).getKey();
Intent profileIntent = new Intent(UsersActivity.this, ProfileActivity.class);
profileIntent.putExtra("visit_user_id", visit_user_id);
startActivity(profileIntent);
}
});
//} else {
//}
}
};
usersList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
我想我解决了新代码,但我会测试性能。
void GetPeoples(){
Users users = new Users();
for(int i =0 ;i<numbers.size();i++){
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Users, UsersViewHolder>
(
Users.class,
R.layout.users_display_layout,
UsersViewHolder.class,
allDatabaseUserreference.orderByChild("phone_number").equalTo(numbers.get(i))
) {
@Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, final int position) {
viewHolder.setUser_name(model.getUser_name());
viewHolder.setUser_status(model.getUser_status());
viewHolder.setUser_thumb_image(getApplicationContext(), model.getUser_thumb_image());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String visit_user_id = getRef(position).getKey();
Intent profileIntent = new Intent(UsersActivity.this, ProfileActivity.class);
profileIntent.putExtra("visit_user_id", visit_user_id);
startActivity(profileIntent);
}
});
}
};
usersList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
}
this is my code ..if any help to this code so use .thank You
package com.example.cloudanalogy.CloudChat;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.flags.Flag;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class ChatsFragment extends Fragment {
private RecyclerView mConvList;
private DatabaseReference mConvDatabase;
private DatabaseReference mMessageDatabase;
private DatabaseReference mUsersDatabase;
private FirebaseRecyclerAdapter friendsRecyclerViewAdapter;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
String data;
public ChatsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_chats, container, false);
mConvList = (RecyclerView) mMainView.findViewById(R.id.conv_list);
mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
Log.i("mCurrent_user_id",mCurrent_user_id);
mConvDatabase = FirebaseDatabase.getInstance().getReference().child("Chat").child(mCurrent_user_id);
mConvDatabase.keepSynced(true);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("user");
mMessageDatabase = FirebaseDatabase.getInstance().getReference().child("messages").child(mCurrent_user_id);
mUsersDatabase.keepSynced(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mConvList.setHasFixedSize(true);
mConvList.setLayoutManager(linearLayoutManager);
fatchChatRecord();
// Inflate the layout for this fragment
return mMainView;
}
//------------------------OnStart method-------------------------------------
@Override
public void onStart() {
super.onStart();
friendsRecyclerViewAdapter.startListening();
}
public void fatchChatRecord()
{
Query conversationQuery=mConvDatabase.orderByChild("timestamp");
FirebaseRecyclerOptions<Conv> options = new FirebaseRecyclerOptions.Builder<Conv>()
.setQuery(conversationQuery, Conv.class)
.build();
friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>((options)) {
@NonNull
@Override
public ConvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_single_layout, parent, false);
return new ConvViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull final ConvViewHolder holder, int position, @NonNull final Conv model) {
final String list_user_id = getRef(position).getKey();
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
data = dataSnapshot.child("message").getValue().toString();
holder.setMessage(data, model.isSeen());
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb image").getValue().toString();
if(dataSnapshot.hasChild("online")) {
String userOnline = dataSnapshot.child("online").getValue().toString();
holder.setUserOnline(userOnline);
}
holder.setName(userName);
holder.setUserImage(userThumb, getContext());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
mConvList.setAdapter(friendsRecyclerViewAdapter);
}
//-------------------------------------------------------------------------ConvViewHolder----------------
class ConvViewHolder extends RecyclerView.ViewHolder {
View mView;
public ConvViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setMessage(String message, boolean isSeen){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(message);
if(!isSeen){
userStatusView.setTypeface(userStatusView.getTypeface(), Typeface.BOLD);
} else {
userStatusView.setTypeface(userStatusView.getTypeface(), Typeface.NORMAL);
}
}
public void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setUserImage(String thumb_image, Context ctx){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.get().load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
public void setUserOnline(String online_status) {
ImageView userOnlineView = (ImageView) mView.findViewById(R.id.user_single_online_icon);
if(online_status.equals("true")){
userOnlineView.setVisibility(View.VISIBLE);
} else {
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
}