gpt4 book ai didi

java - 即使我分配给带有元素的数组列表后,我的数组列表仍然为空?

转载 作者:行者123 更新时间:2023-12-01 19:07:18 25 4
gpt4 key购买 nike

我在FoodListFragment中有一个食物列表,当单击Ok按钮时,我希望将该食物添加到OrderFoodList中,所以我可以在 CartOrderFragment 中显示它,但似乎我不知道如何正确地将数组列表从 FoodListFragment 传递到 CartOrderFragment 因为列表CartOrderFragment 中仍然为空!

这是我想要添加到购物车订单中的食物。

FoodListFragment.java

public class FoodListFragment extends Fragment {

private EditText etSearchInput;
private RecyclerView recyclerView;
private FoodListAdapter foodListAdapter;
private BadgeHolderLayout badgeHolderLayout;
private List<Food> foods;

public interface ListenerFoodListFragment {
void sendToCart(int value, int adapterPosition);
void cartClickListener();
void orderedList(List<Food> foods);
}

private ListenerFoodListFragment listenerFoodListFragment;

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listenerFoodListFragment = (ListenerFoodListFragment) context;
} catch (ClassCastException e) {
Log.d("FoodListFragment", "onAttach: " + e.getMessage());
}
}

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

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerview_foodlist);
badgeHolderLayout = view.findViewById(R.id.badge);
etSearchInput = view.findViewById(R.id.food_list_search_input);
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

foods = new ArrayList<>();
foods.add(new Food("Burger", "1000$", R.drawable.burger));
foods.add(new Food("Burger", "1000$", R.drawable.burger));

foodListAdapter = new FoodListAdapter(getActivity(), foods, listenerFoodListFragment);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(foodListAdapter);
}
}

FoodListAdapter.java

public class FoodListAdapter extends RecyclerView.Adapter<FoodListAdapter.ViewHolder> implements Filterable {

private Context context;
private List<Food> foodList;
private List<Food> foodListFiltered;
private List<Food> foodOrdered;
FoodListFragment.ListenerFoodListFragment listenerFoodListFragment;


public FoodListAdapter(Context context, List<Food> foodList,
FoodListFragment.ListenerFoodListFragment listenerFoodListFragment) {
this.context = context;
this.foodList = foodList;
this.listenerFoodListFragment = listenerFoodListFragment;
this.foodListFiltered = foodList;
this.foodOrdered = new ArrayList<>();
}


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

Food food = foodListFiltered.get(position);


holder.tvFoodTitle.setText(food.getTitle());
holder.tvFoodImage.setImageResource(food.getImage());
holder.tvPrice.setText(food.getPrice());
}

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


}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvOk;

public ViewHolder(@NonNull View itemView) {
super(itemView);

// where i am handling the click
tvOk.setOnClickListener(v -> {
Food food = foodList.get(getAdapterPosition());
foodOrdered.add(food);
listenerFoodListFragment.orderedList(foodOrdered);
});

}
}
}

CartOrdersFragment(我希望订单列表显示在此处)

public class CartOrdersFragment extends Fragment {

private RecyclerView recyclerView;
private CartOrdersAdapter cartOrdersAdapter;
private List<Food> foods;


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

foods = new ArrayList<>();

cartOrdersAdapter = new CartOrdersAdapter(getActivity(), foods);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(cartOrdersAdapter);

}

// receiving the list from the activity
public void getOrdered(List<Food> foodsOrdered) {
foods = foodsOrdered;
}
}

CartOrderAdapter.java

public class CartOrdersAdapter extends RecyclerView.Adapter<CartOrdersAdapter.ViewHolder> {

private Context context;
private List<Food> foods;

public CartOrdersAdapter(Context context, List<Food> foods) {
this.context = context;
this.foods = foods;
}


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

Food food = foods.get(position);

holder.tvTitle.setText(food.getTitle());
holder.ivFood.setImageResource(food.getImage());
holder.tvPrice.setText(food.getPrice());

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements
FoodListFragment.ListenerFoodListFragment {

FragmentManager fm;
FoodListFragment foodListFragment;
CartOrdersFragment cartOrdersFragment = new CartOrdersFragment();


// to pass the list from FoodListFragment to CartOrdersFragment
@Override
public void orderedList(List<Food> foods) {
cartOrdersFragment.getOrdered(foods);
}

}
}

最佳答案

您需要更新购物车适配器列表,而不是 CartFragment 列表。

public void getOrdered(List<Food> foodsOrdered) {
cartOrdersAdapter.setItems(foodsOrdered);
}
}

在购物车订单适配器

void setItems(List<Food> list)) {
foods.clear();
foods.addAll(list);
notifyDataSetChanged();
}

关于java - 即使我分配给带有元素的数组列表后,我的数组列表仍然为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com