我如何从我的一个片段中添加数据,并将其传递到我的适配器,并将其设置到我的阵列列表中?

问题描述 投票:0回答:3

我尝试实现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)

因此,我当前的问题是,我正在将数据从一个Fragment传递到另一个Fragment,并尝试将通过捆绑软件传递的数据添加到我的Adapter,但是我的方法无法正常工作。关于应该做什么而不是将我的捆绑包从一个碎片传递到另一个碎片的任何想法?我正在尝试从AddAnimalFragment抓取文本,并将该数据添加到我的List animalList

这是我的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;
    }
}

这是我的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);
            }
        });
    }

}

java android android-fragments arraylist android-adapter
3个回答
0
投票
将此添加到您的if(bundle!= null){}块中作为最后一行。

0
投票
希望这会有所帮助!

0
投票
所以我找出了问题:Tarik和Nehal都正确,因为我必须将新对象添加到适配器中。我必须修复的是我的动物适配器。由于我的列表被定义为不可变的,因此我必须在AnimalAdapter()中初始化一个新的ArrayList,然后添加默认情况下提供的ArrayList以便使animalList可变!
© www.soinside.com 2019 - 2024. All rights reserved.