Я пытался реализовать animalAdapter.addNewAnimal (animal), но мое приложение вылетает с этой ошибкой:
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)
Поэтому моя текущая проблема заключается в том, что я передаю свои данные из одного фрагмента в другой фрагмент и пытаюсь добавить данные, которые я передал через комплект к моему адаптеру, но мой подход не работает. Любые идеи относительно того, что я должен делать по-другому, вместо того, чтобы передать свой пакет от фрагмента к фрагменту? Я пытаюсь получить текст из AddAnimalFragment и добавить эти данные в мой список animalList
Это мое животное. 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;
}
}
Это мой 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);
}
});
}
}
Это мой 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);
}
}
}
Это мой 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);
}
});
}
}