如何从firebase数据库获取令牌列表?

时间:2017-10-12 13:36:11

标签: android firebase-realtime-database

在下面的代码中,我按其血型和地点获取用户列表。这工作得非常好。我想获得他们的代币清单。那我怎么能这样做呢。我尝试使用firebaseRecyclerAdapter.getItemCount()和循环,但它没有帮助。那我现在该怎么办?我想把令牌放在一个字符串数组中。

enter image description here

public class SearchActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private String bloodgroup;
    private String locality;
    private RecyclerView allUsersList;
    private Query allDatabaseUserReference;
    private String[] TokenList;

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

        bloodgroup = getIntent().getStringExtra("bt");
        locality = getIntent().getStringExtra("pa");

        allDatabaseUserReference = FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("user_search").equalTo(bloodgroup + ", " + locality);


        mToolbar = (Toolbar) findViewById(R.id.search_activity_app_bar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Search Result");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        allUsersList = (RecyclerView) findViewById(R.id.all_users_list);
        allUsersList.setHasFixedSize(true);
        allUsersList.setLayoutManager(new LinearLayoutManager(this));

    }


    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<AllUsers,AllUsersViewHolder> firebaseRecyclerAdapter
                = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>
                (
                        AllUsers.class,
                        R.layout.all_users_display_layout,
                        AllUsersViewHolder.class,
                        allDatabaseUserReference
                )
        {
            @Override
            protected void populateViewHolder(AllUsersViewHolder viewHolder, AllUsers model, final int position) {

                viewHolder.setUser_name(model.getUser_name());
                viewHolder.setUser_blood(model.getUser_blood());
                viewHolder.setUser_mobile(model.getUser_mobile());
                viewHolder.setUser_thumb_image(getApplicationContext(),model.getUser_thumb_image());



                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view)
                    {
                        String visit_user_id = getRef(position).getKey();
                        Intent profileIntent = new Intent(SearchActivity.this, ProfileActivity.class);
                        profileIntent.putExtra("visit_user_id",visit_user_id);
                        startActivity(profileIntent);
                    }
                });

            }
        };



        allUsersList.setAdapter(firebaseRecyclerAdapter);

    }

    public static class AllUsersViewHolder extends RecyclerView.ViewHolder
    {
        View mView;

        public AllUsersViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setUser_name(String user_name)
        {
            TextView name = (TextView) mView.findViewById(R.id.all_users_username);
            name.setText(user_name);
        }

        public void setUser_blood(String user_blood)
        {
            TextView blood = (TextView) mView.findViewById(R.id.all_users_blood);
            blood.setText(user_blood);
        }

        public void setUser_mobile(String user_mobile)
        {
            TextView mobile = (TextView) mView.findViewById(R.id.all_users_phone);
            mobile.setText(user_mobile);
        }

        public void setUser_thumb_image(Context ctx, String user_thumb_image)
        {
            CircleImageView thumb_image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);
            Picasso.with(ctx).load(user_thumb_image).placeholder(R.drawable.default_profile).into(thumb_image);
        }



    }










}

1 个答案:

答案 0 :(得分:0)

您可以在引用childEventListner

上应用allDatabaseUserReference来完成此操作
 allDatabaseUserReference.addChildEventListener(childEventListener);

这是你的childEventListener

 ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
       if(dataSnapshot!=null){
          if(dataSnapshot.hasChild("device_token")){
             tokenList.add(dataSnapshot.child("device_token").getValue().toString());
          }
       }
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {}

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {}

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {}

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};