完整代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_chats, container, false);
noUsersText = (TextView) rootView.findViewById(R.id.noUsersText);
mUsersList = (RecyclerView) rootView.findViewById(R.id.usersList);
mUsersList.addItemDecoration(new DividerItemDecoration(getContext(),
DividerItemDecoration.HORIZONTAL));
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
UID = mAuth.getCurrentUser().getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("UserData");
mProgressDialogue = new ProgressDialog(getActivity());
mProgressDialogue.setMessage("Loading...");
mProgressDialogue.show();
mUsersList.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerOptions<AllUsers> options =
new FirebaseRecyclerOptions.Builder<AllUsers>()
.setQuery(mDatabaseReference, AllUsers.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, HomeScreen_Contacts.UsersViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull HomeScreen_Contacts.UsersViewHolder holder, int position, @NonNull AllUsers model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
holder.setImage(model.getImage());
final String userId = getRef(position).getKey();
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("UserData").child(userId);
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.child("Name").getValue().toString();
Intent intent = new Intent(getActivity(), Chat.class);
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", userName);
startActivity(intent);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
@Override
public HomeScreen_Contacts.UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_users_layout, parent, false);
return new HomeScreen_Contacts.UsersViewHolder(view);
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
return rootView;
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView mDisplayName = (TextView) mView.findViewById(R.id.display_name);
mDisplayName.setText(name);
}
public void setStatus(String status) {
TextView mDisplayStatus = (TextView) mView.findViewById(R.id.display_status);
mDisplayStatus.setText(status);
}
public void setImage(String image) {
CircularImageView mDisplayImage = (CircularImageView) mView.findViewById(R.id.circleImageView);
Picasso.get().load(image).into(mDisplayImage);
}
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
if(firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
我必须使用此片段中的查询顺序
我的代码
final ArrayList<AllUsers> messages = new ArrayList<>();
mDatabaseReference.child(UID).orderByChild("LastMessage").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
messages.add(dataSnapshot.getValue(AllUsers.class));
}
Collections.reverse(messages);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
但是我不知道将这段代码放在哪里...或者这甚至是我想要做的事情的正确方法。请有人帮助我。几天以来一直停留在这里。
答案 0 :(得分:0)
在活动中,我们将onCreate
作为活动开始时运行的方法,并将onCreateView
作为片段中的方法。在活动上启动Firebase侦听器时,我们使用onStart
和onStop
,这与Fragment相同。
将代码放入onCreateView