我使用FirebaseRecyclerAdapter
向RecyclerView
显示数据。只是我在选择数据中做自定义时遇到了麻烦。
我想要显示与whose
相同的数据is not
ID ID of the device currently logged in
。
现在,如果我从服务器获取数据,它将显示two
数据。它应该只是one
数据。因为我不希望用户ID当前登录才会显示
我的 UserActivity:
public class UsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView mUsersList;
private DatabaseReference mUsersDatabase;
private CircleImageView mDisplayImage;
private ProgressBar mLoading;
private FirebaseAuth mAuth;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
Toast.makeText( UsersActivity.this, "MY_LOG : " + currentUser.getUid().toString(), Toast.LENGTH_SHORT ).show();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("User List");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDisplayImage = (CircleImageView) findViewById(R.id.user_single_image);
mLoading = (ProgressBar) findViewById( R.id.loading_activity_user );
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUsersDatabase.keepSynced( true );
mUsersList = (RecyclerView) findViewById(R.id.users_list);
mUsersList.setHasFixedSize(true);
mUsersList.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.users_single_layout,
UsersViewHolder.class,
mUsersDatabase
) {
@Override
protected void populateViewHolder(final UsersViewHolder usersViewHolder, Users users, int position) {
final String user_id = getRef(position).getKey();
usersViewHolder.setDisplayName(users.getName());
usersViewHolder.setStatus(users.getStatus());
usersViewHolder.setThumbImage(users.getThumb_image(), getApplicationContext());
mUsersDatabase.child( user_id ).addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild( "online" )) {
String userOnlineStatus = dataSnapshot.child( "online" ).getValue().toString();
if(userOnlineStatus.equals( "Online" )) {
usersViewHolder.setOnlineIcon( "Online" );
} else {
usersViewHolder.setOnlineIcon( "Offline" );
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
usersViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent profile_intent = new Intent(UsersActivity.this, ProfileActivity.class);
profile_intent.putExtra("user_id", user_id);
startActivity(profile_intent);
}
});
if (position == getItemCount() - 1) {
mLoading.setVisibility( View.INVISIBLE );
mUsersList.setVisibility( View.VISIBLE );
}
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
mUsersList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name) {
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setStatus(String status) {
TextView statusView = (TextView) mView.findViewById(R.id.user_single_status);
statusView.setText(status);
}
public void setThumbImage(final String thumb_image, final Context ctx) {
final CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx)
.load(thumb_image)
.networkPolicy( NetworkPolicy.OFFLINE )
.placeholder(R.drawable.no_profile)
.into( userImageView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx)
.load(thumb_image)
.resize(80, 80)
.placeholder(R.drawable.no_profile)
.into(userImageView);
}
} );
}
public void setOnlineIcon(String online_status) {
ImageView userOnlineView = (ImageView) mView.findViewById( R.id.user_single_online_icon );
if (online_status.equals( "Online" )) {
userOnlineView.setVisibility( View.VISIBLE );
} else {
userOnlineView.setVisibility( View.INVISIBLE );
}
}
}
}
使用该代码,我可以轻松地将其显示在屏幕上的RecyclerView
中。
我得到一个当前ID:
currentUser = mAuth.getCurrentUser().getUid();
如何修改FirebaseRecyclerAdapter
以使用not like command
?
显示的结果是:检索Users
子项中的所有数据,但don't include
当前记录的用户数据。