Passing data through fragments in viewpager

I have a Viewpager with 2 snippets: CurrentWeatherFragmentand ForecastFragment. I need to pass a string from one to another using the interface as shown below, but I keep receiving NullPointerException, the message is not going through properly ...

public class CurrentWeatherFragment extends Fragment {

SendMessage SM

public void onCreateView(...) {
String Message = "Hello"
SM.sendData(Message);
}

interface SendMessage
{
    public  void sendData(String message);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        SM = (SendMessage) activity;
    } catch(ClassCastException e) {
        throw  new ClassCastException("Musisz zaimplementowac metode sendData");
    }
}
}

MainActivity.java

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;


public class MainActivity extends FragmentActivity implements CurrentWeatherFragment.SendMessage {

ViewPager viewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //setting fragment view pager
    viewPager = (ViewPager)findViewById(R.id.pager);
    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
}

public void sendData (String message){
    ForecastFragment FF = new ForecastFragment();
    FF.getData(message);
}

}

ForecastFragment.java

public class ForecastFragment extends Fragment {

public View onCreateView(){
TextView txt = (TextView)v.findViewById(R.id.txt_forecast);
}

public void getData(String message){
        txt.setText(message);
}
}

I successfully used this method in another application, where I had 2 fragments in one action, and I could name them by identifier

public void sendData(String message) {

    SecondFragment f2 = (SecondFragment)getFragmentManager().findFragmentById(R.id.F2);
    f2.getData(message);
}

But here Fragments do not have identifiers, and I think this message is not transmitted because I do not use it FragmentManager(), but how to find a fragment in the viewpager without ID, any suggestions / ideas?

+4
source share
3 answers

, , , :

String tag = "android:switcher:" + R.id.pager + ":" + index;
Fragment f = getSupportFragmentManager().findFragmentByTag(tag);

R.id.pager viewpager , - ( ) Viewpager. , , .

, , LocalBroadcastManager BroadcastReciver , , , , .

+5

.

, , getInstance ( String).

public static YourClass getInstance(String data) 
{
    YourClass object = new YourClass();
    Bundle bundle =  new Bundle();
    bundle.putString(key, data);
    object.setArguments(bundle);  

    return object;
}

onCreate getArguments()

+2

, , viewpager

:

:

:

1) BillDetailFragment

2) ClientDepMonFragment

private TabLayout tabLayout;
    private ViewPager viewPager;


  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_detail, container, false);


// adding  fragment and passing data to it
        BillDetailFragment billDetailFragment=new BillDetailFragment();
        Bundle bundle=new Bundle();
        bundle.putString("mobile",mobile);
        bundle.putInt("c_id",client_id);
        bundle.putInt("server_id",server_id);
        billDetailFragment.setArguments(bundle);

        ClientDepMonFragment clientDepMonFragment=new ClientDepMonFragment();
        Bundle bundle1=new Bundle();
        bundle1.putString("mobile",mobile);
        bundle1.putInt("c_id",client_id);
        bundle1.putInt("server_id",server_id);
        clientDepMonFragment.setArguments(bundle1);


        tabLayout=(TabLayout)view.findViewById(R.id.detail_txn_tab_layout);
        viewPager=(ViewPager)view.findViewById(R.id.detail_txn_viewpager);
        dtViewPagerAdapter=new DtViewPagerAdapter(getChildFragmentManager(),DetailFragment.this);

        dtViewPagerAdapter.addFragments(billDetailFragment,"Lending Money");
        dtViewPagerAdapter.addFragments(clientDepMonFragment,"Deposited Money");
        viewPager.setAdapter(dtViewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
}

BillDetailFragment: onCreate

        private int client_id,server_id;
        private String client_mobile;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            client_mobile=getArguments().getString("mobile");
            client_id=getArguments().getInt("c_id");
            server_id=getArguments().getInt("server_id");
        }
+1
source

All Articles