2017-10-12 68 views
1

在下面的代碼中,我通過他們的血型和地點獲取用戶列表。這工作非常好。我想獲得他們的令牌列表。那我該怎麼做。我嘗試使用firebaseRecyclerAdapter.getItemCount()和一個循環,但它沒有幫助。那麼我現在該做什麼?我想把標記放入一個字符串數組中。如何從Firebase數據庫獲取令牌列表?

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



    } 










} 

回答

0

你可以在你的應用參考做childEventListnerallDatabaseUserReference

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) {} 
}; 
+0

如果需要令牌的陣列? – Haider

+0

當您嘗試通過該查詢獲取用戶列表時,您可以僅在此時並排獲取令牌。這個'onChildAdded'將會爲每個用戶調用,並且每個用戶的令牌只能在那個時候被保存。 – Anmol

+0

而我將如何獲得用戶的UID? 與當前用戶一樣,我們使用「mAuth.getCurrentUser()。getUid();」。 – Haider