Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

android - Passing data beetwen fragments in viewpager

I've Viewpager wit 2 fragments: CurrentWeatherFragment and ForecastFragment. I need to pass string from one to another, Iam using interface like below, but I keep getting NullPointerException, the message is not passing propertly...

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've used this method succesfully in other app where I've had 2 fragments in one activity and i could call them by ID

public void sendData(String message) {

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

But here Fragments dont have IDs and I think that message is not passed because i dont use FragmentManager(), but how to find fragment in viewpager without ID, any suggestion/ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Although a little hacky what you can do is get the fragment by its tag by using the following code:

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

Where R.id.pager is the id of the viewpager in your layout and index is the position (as an integer) in the Viewpager Adapter. I can't say this will work forever but it works for me at the moment.

The Alternative i would suggest is using a LocalBroadcastManager and a BroadcastReciver to send data internally between your fragments as although its a little more work it helps get rid of the spaghetti code situation you may end up finding yourself in trying to reference the fragments directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...