0

How to display data with != filter? I have the data as shown below :

enter image description here

When I retrieve data using FirebaseRecyclerAdapter, both data appear on the screen.

What I want is how to make a query for the data that appears is just another user's data other than the user who logged in the application

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

}

With my code above, the data displayed on the screen is all user data in the database.

Examples of total users are two (Victor and Bertho). If I am logged in as Victor, then the one on the list is Bertho. Vice versa.

I can get the login user ID using the line of code :

currentUser = mAuth.getCurrentUser();

How do I filter them according to the case I want?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Bertho Joris
  • 1,483
  • 5
  • 27
  • 60
  • Possible duplicate of [is it possible query data that are not equal to the specified condition?](https://stackoverflow.com/questions/28582223/is-it-possible-query-data-that-are-not-equal-to-the-specified-condition) – akhilesh0707 Aug 14 '17 at 06:17

1 Answers1

0

To solve this, i recomand you adding a node in your Firebase database, named allUsers. This new node should look like this:

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

Then put a listener on this new node a get all the users. Add all those users in a list. Then extract a radom user like is explained in this post. Don't forget to verify the users to be different than the logged in user. Having this random user, just display the data in your just another user's data section.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193