Firebase选择除数据外

时间:2017-08-14 05:47:57

标签: java android firebase firebase-realtime-database firebase-authentication

如何使用!=过滤器显示数据?我的数据如下所示:

https://github.com/ieatlint/SWipe

当我使用FirebaseRecyclerAdapter检索数据时,两个数据都会显示在屏幕上。

我想要的是如何查询显示的数据just another user's data other而不是用户who logged in the application

我的 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;
    private FirebaseRecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_users );

        mAuth = FirebaseAuth.getInstance();
        currentUser = mAuth.getCurrentUser();

        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();

        mAdapter = 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( mAdapter );
        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 );
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter.cleanup();
    }

    @Override
    public void onStop() {
        super.onStop();
        mAdapter.cleanup();
    }

}

使用上面的代码,屏幕上显示的数据在数据库中为all user data

总用户的例子有两个(Victor和Bertho)。如果我以Victor身份登录,那么列表中的人就是Bertho。反之亦然。

我可以使用代码行获取登录用户ID:

currentUser = mAuth.getCurrentUser();

如何根据我想要的情况过滤它们?

1 个答案:

答案 0 :(得分:0)

要解决此问题,我重新命令您在Firebase数据库中添加一个名为allUsers的节点。这个新节点应如下所示:

Firebase-root
    |
    --- allUsers
          |
          --- 8xyd... : "Victor"
          |
          --- 3kWv... : "Bertho"
          |
          --- 7Gzo... : "Other User"

然后在这个新节点上放一个监听器来获取所有用户。在列表中添加所有这些用户。然后提取一个radom用户,就像在post中解释的那样。不要忘记验证用户与登录用户的不同。拥有此随机用户,只需在just another user's data部分显示数据。

希望它有所帮助。