无法使用bundle从一个片段到另一个片段获取tabhost的位置

时间:2015-07-02 03:02:30

标签: android android-fragments android-tabhost android-tabs

我有两个片段。一个用于选项卡,一个用于调用列表视图的主布局。我无法将“位置”从我的AlarmTab类传递到我的AlarmActivity类。该位置正在通过,但它只获得我的数组的长度。当我从我的位置[0]选项卡中单击一个列表时,它返回myTabHost的长度而不是单击的选项卡。我想获得点击的位置以及下面是我到目前为止所做的代码。我已经用了几个小时了。希望有人可以帮助我指出我所缺少的东西。先感谢您!

来自AlarmTab Class的

mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
widget = (TabWidget) rootView.findViewById(android.R.id.tabs);
hs = (HorizontalScrollView) rootView.findViewById(R.id.horizontalScrollView);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

for(int i=0; i<SmashDeviceData.get_instance().devices.size(); i++) {

    AlarmActivity alarmActivity = new AlarmActivity();
    Bundle bundle = new Bundle();
    bundle.putInt("position", i);
    alarmActivity.setArguments(bundle);
    mTabHost.addTab(mTabHost.newTabSpec("i").setIndicator(SmashDeviceData.get_instance().devices.get(i).deviceName), AlarmActivity.class, bundle);
    } 

来自AlarmActivity Class

   public void addAlarm(){
        ImageView addAlarmButton = (ImageView) getActivity().findViewById(R.id.add_alarm);
        addAlarmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivity mainDrawer = (FragmentActivity) context;
                Fragment fragment;
                Bundle b = getArguments();
                int position = b.getInt("position", 0);
                Toast.makeText(context, String.valueOf(b.getInt("position")), Toast.LENGTH_SHORT ).show();
                fragment = AlarmSetActivity.newInstance(context, position);
                FragmentManager fragmentManager = mainDrawer.getSupportFragmentManager();
                FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.replace(R.id.content_frame, fragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

你的问题在于这一行

    mTabHost.addTab(mTabHost.newTabSpec("i").setIndicator(SmashDeviceData.get_instance().
    devices.get(i).deviceName), AlarmActivity.class, bundle);

应该是这样的

    mTabHost.addTab(mTabHost.newTabSpec(SmashDeviceData.get_instance().
    devices.get(i).deviceName).setIndicator(SmashDeviceData.get_instance().devices.get(i).
    deviceName), AlarmActivity.class, bundle);