0

I have created fragment activity using viewpager with two tabs, and created listview with two textviews. I want to sent the two texts from tab one to tab two as title and description. I have successfully sent the description, but I failed to send the title (]I can only send one text to listview).

My fragment_one.java :

public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    viewPager = (ViewPager)view.findViewById(R.id.viewPager);
    Button btnPassData = (Button) view.findViewById(R.id.btnPassData);

    final ListView list=(ListView)view.findViewById(R.id.list_view);
    final EditText inData = (EditText) view.findViewById(R.id.inMessage);
    btnPassData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SM.sendData(inData.getText().toString().trim());
        }
    });

}

interface SendMessage {
    void sendData(String message);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        SM = (SendMessage) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException("Error in retrieving data. Please try again");
    }
}
}

my fragment_two.java :

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_two, container, false);
    return rootView;


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    listView = (ListView) view.findViewById(R.id.list_view);
    adapter = new ArrayAdapter<String>(getActivity(), R.layout.single_item,R.id.tvdesc, arrayList);

    listView.setAdapter(adapter);
}

protected void displayReceivedData(String message) {
    arrayList.add(0,message);
    adapter.notifyDataSetChanged();

}
}
Steampunkery
  • 3,839
  • 2
  • 19
  • 28
  • https://stackoverflow.com/questions/46600853/how-to-make-this-code-to-accept-sending-and-receiving-another-text – Mike M. Oct 07 '17 at 21:39
  • @zazer Take a look at this link https://stackoverflow.com/questions/24555417/how-to-send-data-from-one-fragment-to-another-fragment – Vector Oct 08 '17 at 03:22

1 Answers1

0

You should use call back data from fragment_one to main activity , and send data from activity to fragment_two (and refresh view)

Callback fragment to activity you can use How to make a callback between Activity and Fragment?

and Fragment_two you can set this value in Activity, EX, private Fragment mFragmentTwo; ......... mFragmentTwo= new Fragment();

and can set value by funtion : mFragmentTwo.displayReceivedData(yourString)