我将这个片段作为我的应用程序的一部分,当我点击主屏幕上导航抽屉中的按钮打开片段时,它开始无限地调用super.onBindViewHolder()
行并且永远不会突破它,所以它只会杀死活动并切换回主屏幕。我已经使用调试器来确定这实际上是发生了什么,我只是不知道为什么。在我添加" read"之前,它没有这样做。数据库规则的权限。它正在加载片段但没有显示任何数据,因为它没有读取权限,所以我添加了它,现在它正在做我上面描述的。
这是片段的代码:
package com.jggdevelopment.wannacook.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatDelegate;
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.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.jggdevelopment.wannacook.AddItemDialogFragment;
import com.jggdevelopment.wannacook.InventoryAdapter;
import com.jggdevelopment.wannacook.InventoryItem;
import com.jggdevelopment.wannacook.MyViewHolder;
import com.jggdevelopment.wannacook.R;
import java.util.ArrayList;
/**
* Handles actions taken inside the "Fridge" tab of the app.
* Created by Cameron on 1/18/2018.
*/
public class FridgeFragment extends Fragment {
private ArrayList<InventoryItem> items = new ArrayList<>();
private static final String TAG = "FridgeFragement";
private FirebaseRecyclerAdapter<InventoryItem, FridgeHolder> firebaseRecyclerAdapter;
private RecyclerView rvItems;
public FridgeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fridge, container, false);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("users");
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = rootRef.child(uid).child("fridge");
//Lookup recycler view in activity layout
rvIt
ems = view.findViewById(R.id.fridge_list);
// Set layout manager to position the items
rvItems.setLayoutManager(new LinearLayoutManager(getContext()));
FirebaseRecyclerOptions<InventoryItem> options = new FirebaseRecyclerOptions.Builder<InventoryItem>()
.setQuery(query, InventoryItem.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<InventoryItem, FridgeHolder>(options) {
@NonNull
@Override
public FridgeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inventory_card, parent, false);
return new FridgeHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull FridgeHolder holder, int position, @NonNull InventoryItem item) {
super.onBindViewHolder(holder, position);
holder.setItem(item);
Log.d("TAG", item.getItemName());
}
};
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
return view;
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
rvItems.setAdapter(firebaseRecyclerAdapter);
}
@Override
public void onStop() {
super.onStop();
if (firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
private class FridgeHolder extends RecyclerView.ViewHolder {
private final ImageView image;
private final TextView itemName;
private final TextView itemQuantity;
public FridgeHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.item_photo);
itemName = itemView.findViewById(R.id.item_name);
itemQuantity = itemView.findViewById(R.id.item_quantity);
}
public void setItem(InventoryItem item) {
itemName.setText(item.getItemName());
itemQuantity.setText(item.getQuantity());
}
}
}
以及我的数据库的规则和权限及其结构:
数据库结构:
数据库权限: