android将数据发送到活动中的片段

时间:2014-02-01 00:34:23

标签: java android

我有一个地图活动。当我单击标记的信息窗口时,intent会将id发送到我的片段。在我的地图活动中:

map.setOnInfoWindowClickListener(
    new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker m) {
            Intent nextScreen = new Intent (Map.this,
            PHDetail.class);
            Bundle bundle = new Bundle();
            bundle.putString("test", "id");
            Callfor c = new Callfor();
            c.setArguments(bundle);
            startActivityForResult(nextScreen, 101);
        }
    });

在我的片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_call_for, container, false);
    TextView TXT = (TextView) rootView.findViewById(R.id.test);
    Bundle test = getArguments();
    if(test!=null){
        String d = test.getString("test", " "); 
        TXT.setText(d);
    }
    return rootView;
}

问题是bundle是null。我该如何解决这个问题?

这是我的PHDetail FragmentActivity类:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.phdetail);
            Intent intent = getIntent();
    if (intent != null) {
        String id = intent.getStringExtra("test");
    }

            // Initialization :
            viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter  = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs :
        for (String tab_name : tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(this));
        }
        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view

        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {


    }

}

1 个答案:

答案 0 :(得分:1)

<强>更新

在你的onTabSelected上你需要创建你的片段,设置包然后像这样进行交易。

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view

    viewPager.setCurrentItem(tab.getPosition());
    Bundle bundle = new Bundle();
    bundle.putString("id", id);
    MyFragment fragment=new MyFragment();
    fragment.setArguments(bundle);

    ft = getFragmentManager().beginTransaction();
    ft.add(R.id.fragment_container,fragment);
    ft.commit();

}

第一个答案

将值传递给其他活动(如FragmentActivity)的正确方法是将这些值添加到启动它们的intent中。

map.setOnInfoWindowClickListener(
    new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker m) {
            Intent nextScreen = new Intent (Map.this, PHDetail.class);
            intent.putExtra("test", "id");
            startActivityForResult(nextScreen, 101);
        }
    });

然后在名为 PHDetail 的FragmentActivity上,您应该从Intent中获取值。

Intent intent = getIntent();
if (intent != null) {
    String id = intent.getStringExtra("test");
}

之后,当您在PHDetail上的容器视图上加载Fragment时,您应该添加Bundle然后执行事务。

Bundle bundle = new Bundle();
bundle.putString("test", "id");
Callfor c = new Callfor();
c.setArguments(bundle);

之后,您将能够像上面所述检索Fragment中的值

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle         savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_call_for, container, false);
    TextView TXT = (TextView) rootView.findViewById(R.id.test);
    Bundle test = getArguments();
    if(test!=null){
        String d = test.getString("test", " "); 
        TXT.setText(d);
    }
    return rootView;
}    

希望它有所帮助! :)