1

I need help on understanding how the following scenario works and how to achieve the result.

I have a class called ShoppingCart. It has a method called addItemsToShoppingCartFromPreviousOrder. Now I have a fragment called PreviousOrderFragment with onCreateView method which is using the RecyclerView. Its a list of previous orders. I have multiple lists. Now I want to know how to call the addItemsToShoppingCartFromPreviousOrder method from ShoppingCart into my fragment. so that when the user clicks a list, it will add to the cart.

Roadies
  • 3,309
  • 2
  • 30
  • 46
Ronan
  • 64
  • 1
  • 12
  • 2
    If you do want to save your time, use EventBus instead https://github.com/greenrobot/EventBus – kaitian521 Jul 21 '16 at 11:17
  • The method called `addItemsToShoppingCartFromPreviousOrder` does the job. I have to know how to call or where to call that method. For example, on my fragment `onCreateView` I added the `onClickListner` won't do nothing. And I cannot use the third party library. – Ronan Jul 21 '16 at 11:19
  • Possible duplicate of [Pass data between fragments](http://stackoverflow.com/questions/38452210/pass-data-between-fragments) – TapanHP Jul 21 '16 at 12:40

3 Answers3

1

You need do that:

In you Adapter of RecycleView put:

  private ItemListener mListener;
  ...
  public void setmListener(ItemListener mListener) {
     this.mListener = mListener;
  }
  ...
  public interface ItemListener {
     void onItemSelected(Item item);
  }

Item = The item selected

them... In your ViewHolder of Adapter put that:

 public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView tv_id;
        public TextView tv_url;
        public TextView tv_login;

        public Holder (View itemView) {
            super(itemView);
            tv_id = (TextView) itemView.findViewById(R.id.tv_id);
            tv_url = (TextView) itemView.findViewById(R.id.tv_url);
            tv_login = (TextView) itemView.findViewById(R.id.tv_login);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                Integer position = Integer.valueOf(getLayoutPosition());
                mListener.onUserSelected(mList.get(position));
            }
        }
    }

OBS: That ViewHolder is a InnerClass of Adapter

Them all you need do is:

In your fragment

mRecycleViewAdapter.setmListener(this)

Them make your fragment:

public class MyFragment extends Fragment implements MyRecycleViewAdapter.ItemListener

And override :

 @Override
    public void onItemSelected(Item item) {
        //call addItemsToShoppingCartFromPreviousOrder 
    }

Here is a example, see SearchFragment.java and UsersListAdapter.java

Hugo Landim
  • 169
  • 8
0

1- Create a interface call it for ex. OnItemClicked

Interface OnItemClick{
    void onItemClicked(/*pass your selected object*/);
}

2- make the fragment implement this interface

Class PreviousOrderFragment extends Fragment implements OnItemClicked{
 void onItemClicked(/*pass your selected object*/){
        /** here you have the select object when item clicked from the activity **/
         addItemsToShoppingCartFromPreviousOrder(); 
 }
}

3- Take an instance from the fragment in the Activity

Class YOUR_Activity extends Activity {
  PreviousOrderFragment fragment = new PreviousOrderFragment ();
  /// in the onClick event 
  public onClick(View v){
    fragment.onItemClicked(/**pass the selected object*/);
  }

}
Ahmed Garhy
  • 475
  • 3
  • 11
0

see this answer, Already explained all the possible ways to make communication between fragments.

But a good way is to use EventBus when you have complicated communication between Many Activities and Fragments, It's much easy and fast,and a better approach then any of this described in above linked answer.

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66