0

i have a food list in FoodListFragment and when the Ok button gets clicked i want that food to be added to an OrderFoodList so i can display it in the CartOrderFragment, but it seems i don't know how to pass that array list from FoodListFragment to CartOrderFragment correctly because the list in CartOrderFragment is still empty!

this is where the foods are that i want to add to my cart orders.

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 ( i want the order list to show up here)

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

}
}
Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
hewa jalal
  • 952
  • 11
  • 24
  • call `cartOrdersAdapter.notifyDataSetChanged();` in `getOrdered` method after adding items in datasource – ρяσѕρєя K Dec 30 '19 at 04:57
  • i changed the method as you asked to this `public void getOrdered(List foodsOrdered) { foods = foodsOrdered; cartOrdersAdapter.notifyDataSetChanged(); }` but now i get NPE like this `java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.foodmenu.fragments.CartOrdersAdapter.notifyDataSetChanged()' on a null object reference` – hewa jalal Dec 30 '19 at 05:03
  • where you are adding `CartOrdersFragment` Fragment to Activity? – ρяσѕρєя K Dec 30 '19 at 05:06
  • when i click on a cart icon in `FoodListFragment` like this `badgeHolderLayout.setOnClickListener(v -> listenerFoodListFragment.cartClickListener());` in activity `@Override public void cartClickListener() { fm.beginTransaction() .replace(R.id.main_container, new CartOrdersFragment()) .addToBackStack(null) .commit(); }` but i get NPE when i click on the ok button. – hewa jalal Dec 30 '19 at 05:10
  • hiwa, problem is when `getOrdered` method is called from Activity `CartOrdersFragment` Fragment is not visible to user. so `cartOrdersAdapter ` is null. my suggestion is instead of using event in this case. just pass `foods` list when you are adding Fragment using `setArguments`. let me know if you have any issue using `setArguments` – ρяσѕρєя K Dec 30 '19 at 05:14
  • well yeah that makes sense, and i guess i have to learn about fragment arguments now but i always had trouble differentiating `setArguments` with interface callbacks i mean if i can send data with those then what the point of interfaces? or interfaces are used to show fragment in activity ? anyway do i need to change anything else? – hewa jalal Dec 30 '19 at 05:19
  • I think everything is fine. to use `setArguments steps: 1. ` make `Food` `serializable or parcelable` 2. call `getArguments()` method in `onActivityCreated ` of `CartOrdersFragment` to get List. see [How to pass custom object arraylist from activity to fragment with instance](https://stackoverflow.com/questions/41376246/how-to-pass-custom-object-arraylist-from-activity-to-fragment-with-instance) – ρяσѕρєя K Dec 30 '19 at 05:24
  • you are so helpful, thank you, will see what i can do about it. – hewa jalal Dec 30 '19 at 05:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205088/discussion-between-hiwa-jalal-and--k). – hewa jalal Dec 30 '19 at 15:22

1 Answers1

0

You need to update the cart adapter list, not the CartFragment list.

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

In CartOrdersAdapter

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