问题
I am trying to retrieve data from my Firebase database, but my screen is not showing anything. I don't see any errors so I don't know why nothing is happening. Can anyone spot any mistakes? I followed the FirebaseUI guide with some help from a stackoverflow member. The data should show the street and the user on the screen
Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_stopandmap, container, false);
Query keyQuery = FirebaseDatabase.getInstance().getReference("/Addresses/Street 10/users");
DatabaseReference dataRef = FirebaseDatabase.getInstance().getReference("/User");
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
.setIndexedQuery(keyQuery, dataRef, User.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<User, BetViewHolder>(options) {
@Override
public BetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_row, parent, false);
return new BetViewHolder(view);
}
@Override
protected void onBindViewHolder(BetViewHolder holder, int position, User model) {
holder.setUserName(model.getUsername());
}
};
recyclerView.setAdapter(adapter);
return rootView;
}
public static class BetViewHolder extends RecyclerView.ViewHolder {
TextView tw;
public BetViewHolder(View itemView) {
super(itemView);
tw = (TextView) itemView.findViewById(R.id.usersTextview);
}
public void setUserName(String username) {
tw.setText("Hej" + username);
Log.e("Activity", "In Vehicle " + username);
}
}
User class:
public class User {
private String username;
private String usertype;
public User() {
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
}
回答1:
In version 3.0 of FirebaseUI, you need to explicitly call startListening() & stopListening() on the adapter to instruct it to start/stop retrieving data from the database.
Adapter Lifecycle - in previous versions the adapters began listening immediately upon instantiation and had a
cleanup()
call to stop listening. In 3.x you must explicitly callstartListening()
andstopListening()
or pass aLifecycleOwner
to the options builder.Source: Upgrading to FirebaseUI 3.0
来源:https://stackoverflow.com/questions/46744498/android-recyclerview-not-working