0

I have this fragment as part of my app, and when I click on the button in the nav drawer on the main screen to open the fragment, it starts calling the super.onBindViewHolder() line infinitely and never breaks out of it, so it just kills the activity and switches back to the main screen. I've used the debugger to determine that this is in fact what is happening, I just don't know why. It wasn't doing this until I added the "read" permission to the database rules. It was loading the fragment but not displaying any data because it didn't have the read permissions, so I added that and now it's doing what I described above.

This is the code for the fragment:

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

and the rules and permissions for my database, along with its structure:

Database Structure:

enter image description here

Database Permissions:

enter image description here

Cameron
  • 1,281
  • 1
  • 19
  • 40
  • It is obvious.... You are calling wrong super method... Which is implemented to call your method...and yeah passed parameters matter in which method would be called.. – Selvin Apr 16 '18 at 17:25
  • It isn't obvious, that's why I asked. When I add the "item" parameter to the call to super.onBindViewHolder(), it says "abstract method onBindViewHolder(VH, int, T) cannot be accessed directly. Any idea how to fix that? – Cameron Apr 16 '18 at 17:37
  • Obviously it super method is abstract then there is no need (and obviously you can't) call it in overrided method... – Selvin Apr 16 '18 at 17:39
  • Look, if it was obvious then I wouldn't have needed to ask the question in the first place. I didn't think that would work but that is what you suggested, to call it with different parameters, so that's what I did. I don't really need it to do anything on binding, should I just leave it blank? – Cameron Apr 16 '18 at 17:50
  • No, why blank? leave holder.setItem(item) where you are doing real bind... Just do not call any super.onBindViewHolder()... – Selvin Apr 16 '18 at 17:55
  • That crashed the app when trying to set the item quantity. String resource exception is the error it gives – Cameron Apr 16 '18 at 17:59
  • Never mind, I got it, thanks for the help. – Cameron Apr 16 '18 at 18:05
  • You cannot use TextView.setText(randomInt)... Because android expects to randomInt be a resource id ... – Selvin Apr 16 '18 at 18:05

0 Answers0