简体   繁体   中英

Android my own listener

I have an ActionBar and it has Tabs. In the MainActivity there is a String variable. In one Tab there is a TextView and a method setTv(String string) what can change the text in it.

I would like to make a Listener interface what can call the setTv(String string) method, when the str String is changes in the MainActivity.

Could anybody help me and fill in this code with the implementation?

This is the MainActivity code:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, LocationListener{

    ActionBar actionbar;
    ViewPager viewpager;
    FragmentPageAdapter ft;
    String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());

        actionbar = getActionBar();
        viewpager.setAdapter(ft);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setText("Run").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("Map").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("Statistics").setTabListener(this));
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            

        });

}

This is the Fragment code:

public class RunFragment extends Fragment {

    TextView tv;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.run_layout, container, false);
        tv =(TextView)view.findViewById(R.id.textView1);

        return view;

    }


    public void setTv(String string){
        tv.setText(string);
    }

}

Get your current fragment in your viewpager:

RunFragment fragment = (RunFragment) ft.getItem(viewPager.getCurrentItem());

Call your method where you want(after change str):

fragment.setTv("string");

For the best way i recommend to you make a class that extends Application

from the docs :

Base class for those who need to maintain global application state

So you can store your str variable in this class and notify any launched Activities of variable state change. Actually i recommend to use Observer pattern

MyApplication :

public class MyApplication extends Application {
    private static MyApplication singleton;
    private String str;
    private ArrayList<StringObserver> observerList = new ArrayList<StringObserver>();
    public MyApplication getInstance(){
        return singleton;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        singleton = this;
    }

    public void setString(String str) {
        this.str = str;
        notifyObservers();
    }

    public void addObserver(StringObserver obs) {
        observerList.add(obs);
    }

    public void removeObserver(StringObserver obs) {
        observerList.remove(obs);
    }

    private void notifyObservers() {
        for(StringObserver obs : observableList) {
            obs.notifyAboutStringChanged(str);
        }
    }
}

StringObserver :

public interface StringObserver {
    void notifyAboutStringChanged(String str);
}

And usage :

public class MainActivity extends FragmentActivity/*or Activity*/ implements StringObserver {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MyApplication appState = ((MyApplication )this.getApplication());
        appState.addObserver(this);
        appState.setString("This string was changed");
    }

    @Override
    public void notifyAboutStringChanged(String str) {
     // do something
    }

}

In other Activity/Fragment :

public class RunFragment extends Fragment {

    TextView tv;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.run_layout, container, false);
        tv =(TextView)view.findViewById(R.id.textView1);

        return view;

    }


    public void setTv(String string){
        tv.setText(string);
        (MyApplication) getActivity().getApplication().setString(string); // this string notify your activity about value change.
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM