0

I tried implementing animalAdapter.addNewAnimal(animal) but my App crashes with this error:

 Process: com.example.navigationcomponentexercise, PID: 15974
    java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:148)
        at java.util.AbstractList.add(AbstractList.java:108)
        at com.example.navigationcomponentexercise.animal.AnimalAdapter.addNewAnimal(AnimalAdapter.java:61)
        at com.example.navigationcomponentexercise.animal.AnimalFragment.onCreateView(AnimalFragment.java:62)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:310)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2222)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1995)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1951)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7073)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

So my current Issue is that I am passing my data from one Fragment to another Fragment, and trying to add the data I passed through the bundle to my Adapter, but my approach is not working. Any ideas as to what I should do differently instead of passing my bundle from fragment to fragment? I am trying to grab the text from AddAnimalFragment, and add that data to my List animalList

This is my Animal.java

public class Animal {

    private String name;
    private String deets;

    public Animal(String name, String deets) {

        this.name = name;
        this.deets = deets;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeets() {
        return deets;
    }

    public void setDeets(String deets) {
        this.deets = deets;
    }
}

This is my AnimalFragment.java

public class AnimalFragment extends Fragment {

    private RecyclerView rvAnimals;
    private FloatingActionButton fabAddNewAnimal;
    private AnimalAdapter animalAdapter;
    private TextView tvAnimalName, tvDeets;
    List<Animal> animalList = new ArrayList<>();
    public AnimalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_animal, container, false);
        fabAddNewAnimal = view.findViewById(R.id.fabNewAnimal);
        rvAnimals = view.findViewById(R.id.rvAnimals);
        rvAnimals.setLayoutManager(new LinearLayoutManager(requireContext()));
        animalAdapter = new AnimalAdapter();
        rvAnimals.setAdapter(animalAdapter);
        tvAnimalName = view.findViewById(R.id.tvAnimalName);
        tvDeets = view.findViewById(R.id.tvDeets);
        Bundle bundle = this.getArguments();
        if(bundle !=null){
            String name = bundle.getString("animal");
            String deets = bundle.getString("deets");
            Animal animal = new Animal(name, deets);
            animal.setName(name);
            animal.setDeets(deets);
        }


        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fabAddNewAnimal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final NavController navController = Navigation.findNavController(view);
                navController.navigate(R.id.action_animalFragment_to_addAnimalFragment);

            }
        });
    }
}

This is my AnimalAdapter.java

public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder> {

    private List<Animal> animalList;



    public AnimalAdapter(){
        animalList = Arrays.asList(
                new Animal("Johny", "Johny is a lil doggy"),
                new Animal("Zabomafoo", "Ahh yes the animal who turns into a clay dude"),
                new Animal("Clifford", "Clifford was cute! That gal was lucky to have a huge red dog like him"),
                new Animal("Courage", "Omg Another one of my favorite childhood cartoon pets! He was so adorable, especially his love for murial"),
                new Animal("Garfield", "All that fat cat did was eat lasagna and complain about Mondays")
        );
    }
    @NonNull
    @Override
    public AnimalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.animal_view, parent, false
        );

        return new AnimalViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AnimalViewHolder holder, int position) {
        Animal animal = animalList.get(position);
        holder.tvAnimalName.setText(animal.getName());
        holder.tvDeets.setText(animal.getDeets());
        //holder.tvAnimalName.setText(animal.setName("Banana"));
    }

    @Override
    public int getItemCount() {
        return animalList.size();
    }

    public void addNewAnimal(Animal animal) {
        animalList.add(animal);
        notifyItemInserted(animalList.size() - 1);
    }

    public class AnimalViewHolder extends RecyclerView.ViewHolder {
        TextView tvAnimalName, tvDeets;
        public AnimalViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAnimalName = itemView.findViewById(R.id.tvAnimalName);
            tvDeets = itemView.findViewById(R.id.tvDeets);
        }
    }




}

This is my AddAnimalFragment.java

public class AddAnimalFragment extends Fragment {
    private FloatingActionButton fabAddNewAnimal;
    EditText newAnimal, newDeets;

    public AddAnimalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_add_animal, container, false);
        fabAddNewAnimal = view.findViewById(R.id.fabNewAnimal);
        newAnimal = view.findViewById(R.id.etAnimal);
        newDeets = view.findViewById(R.id.etDeets);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fabAddNewAnimal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                String animal = newAnimal.getText().toString();
                String deets = newDeets.getText().toString();
                bundle.putString("animal", animal);
                bundle.putString("deets", deets);
                final NavController navController = Navigation.findNavController(view);
                navController.navigate(R.id.action_addAnimalFragment_to_animalFragment, bundle);
            }
        });
    }

}

3 Answers3

0

In your AnimalFragment, you forgot to call

animalAdapter.addNewAnimal(animal);

Add this in your if(bundle !=null){} block as the last line.

Md Tarik Mahmud
  • 271
  • 2
  • 7
0

You have not added animal object to adapter,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_animal, container, false);
        fabAddNewAnimal = view.findViewById(R.id.fabNewAnimal);
        rvAnimals = view.findViewById(R.id.rvAnimals);
        rvAnimals.setLayoutManager(new LinearLayoutManager(requireContext()));
        animalAdapter = new AnimalAdapter();
        rvAnimals.setAdapter(animalAdapter);
        tvAnimalName = view.findViewById(R.id.tvAnimalName);
        tvDeets = view.findViewById(R.id.tvDeets);
        Bundle bundle = this.getArguments();
        if(bundle !=null){
            String name = bundle.getString("animal");
            String deets = bundle.getString("deets");
            Animal animal = new Animal(name, deets);
            animal.setName(name);
            animal.setDeets(deets);
           animalAdapter.addNewAnimal(animal); // add this line
        }


        return view;
    }

Hope this will help!!

Nehal Godhasara
  • 727
  • 1
  • 7
0

So I figured out the issue: Md Tarik and Nehal were both correct, on me having to add the new object to my adapter. What I had to fix was my animal adapter. Since my List was being defined as immutable I had to initialize a new ArrayList in my AnimalAdapter(), and then add the ArrayList I provided by default in order to make the animalList mutable!