嘿伙计们,我尝试根据所选用户更改Firebase中的查询。但是当我更新查询时,回收器视图停止显示数据。下面是我的代码。如果有人能看一看,我将不胜感激。我使用的是 Firebase 提供的 Recyclerview。我使用的数据库是Firestore。
package com.larinet.veikkokrypczyk.myfamilyandfriends;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* A simple {@link Fragment} subclass.
*/
public class LocationListFragment extends Fragment {
ProgressBar progressBar;
RecyclerView locationList;
private static final String TAG = "LocationListFragment";
private FirebaseFirestore db;
private FirestoreRecyclerAdapter adapter;
LinearLayoutManager linearLayoutManager;
private FirebaseAuth mAuth;
private FirebaseUser currentUser;
private Query query;
private FirestoreRecyclerOptions<LocationEntity> response;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location_list, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
db = FirebaseFirestore.getInstance();
progressBar = view.findViewById(R.id.progress_bar);
locationList = view.findViewById(R.id.locationdata_rv);
linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
locationList.setLayoutManager(linearLayoutManager);
currentUser = mAuth.getCurrentUser();
UserEntity user = new UserEntity(currentUser.getDisplayName(), currentUser.getUid());
getInitialData(user);
}
public void getInitialData(UserEntity user){
//FAILED_PRECONDITION: The query requires an index. You can create it here:
query = db.collection("locations").orderBy("date", Query.Direction.DESCENDING).whereEqualTo("id", user.getId());
response = new FirestoreRecyclerOptions.Builder<LocationEntity>()
.setQuery(query, LocationEntity.class)
.build();
attachRecyclerViewAdapter();
adapter.notifyDataSetChanged();
}
public void getLocationDataForUser(UserEntity user){
//FAILED_PRECONDITION: The query requires an index. You can create it here:
query = db.collection("locations").orderBy("date", Query.Direction.DESCENDING).whereEqualTo("id", user.getId());
response = new FirestoreRecyclerOptions.Builder<LocationEntity>()
.setQuery(query, LocationEntity.class)
.build();
adapter.notifyDataSetChanged();
}
private void attachRecyclerViewAdapter() {
adapter = new FirestoreRecyclerAdapter<LocationEntity, FriendsHolder>(response) {
@Override
public void onBindViewHolder(FriendsHolder holder, int position, LocationEntity model) {
progressBar.setVisibility(View.GONE);
holder.locationLat.setText(Double.toString(model.getGeoPoint().getLatitude()));
holder.locationLong.setText(Double.toString(model.getGeoPoint().getLongitude()));
holder.userName.setText(model.getUserName());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm", Locale.GERMAN);
String time = sdf.format(model.getDate());
holder.date.setText(time);
holder.itemView.setOnClickListener(v -> {
Snackbar.make(locationList, Double.toString(model.getGeoPoint().getLatitude())+", "+Double.toString(model.getGeoPoint().getLongitude()), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
});
}
@Override
public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.recyclerview_row, group, false);
return new FriendsHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}
};
locationList.setAdapter(adapter);
}
public class FriendsHolder extends RecyclerView.ViewHolder {
TextView locationLat;
TextView locationLong;
TextView userName;
TextView date;
public FriendsHolder(View itemView) {
super(itemView);
locationLat = itemView.findViewById(R.id.locationLat);
locationLong = itemView.findViewById(R.id.locationLong);
userName = itemView.findViewById(R.id.userName);
date = itemView.findViewById(R.id.date);
}
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
所以我的解决方案现在如下所示:
public void getInitialData(UserEntity user){
//FAILED_PRECONDITION: The query requires an index. You can create it here:
query = db.collection("locations").orderBy("date", Query.Direction.DESCENDING).whereEqualTo("id", user.getId());
response = new FirestoreRecyclerOptions.Builder<LocationEntity>()
.setQuery(query, LocationEntity.class)
.build();
attachRecyclerViewAdapter();
adapter.notifyDataSetChanged();
}
public void getLocationDataForUser(UserEntity user){
adapter.stopListening();
//FAILED_PRECONDITION: The query requires an index. You can create it here:
query = db.collection("locations").orderBy("date", Query.Direction.DESCENDING).whereEqualTo("id", user.getId());
response = new FirestoreRecyclerOptions.Builder<LocationEntity>()
.setQuery(query, LocationEntity.class)
.build();
attachRecyclerViewAdapter();
adapter.startListening();
adapter.notifyDataSetChanged();
}
重要的部分似乎是停止侦听并开始自己侦听,如果您更改查询,我还需要重新实例化适配器。老实说,我不知道所有这些是否都是好的做法,但它有效。