java - 使用选项卡式 Activity 中的 bundle 将值从 Activity 传递到 fragment

标签 java android bundle

我是一个java文盲,但仍在尝试开发一个供我个人使用的应用程序。 我从 android-studio 的“Tabbed-Activity”开始,除了 MainActivity 中的一个 fragment 和一个包之外,大部分没有改变。

这是我的代码:

主要 Activity

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
    private static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //FloatingActionButton fab = findViewById(R.id.fab);
        Bundle bundle = new Bundle();
        bundle.putDouble("loclat", 25.4358);
        bundle.putDouble("loclang",81.8463);
        Fragment SunFragment = new SunFragment();
        SunFragment.setArguments(bundle);

        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
    }
}

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    @StringRes
    private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3};
    private final Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position) {
            case 0:
                return new SunFragment();
            //return PlaceholderFragment.newInstance(pos + 5);
            case 1:
                return PlaceholderFragment.newInstance(1);
            //return SecondFragment.newInstance();
            //return PlaceholderFragment.newInstance(pos + 1);
            default:
                return PlaceholderFragment.newInstance(2);
        }
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mContext.getResources().getString(TAB_TITLES[position]);
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 3;
    }
}

最后,SunFragment,我希望从 MainActivity 中获取 bundle 数据:

public class SunFragment extends Fragment {

    List<SunSession> sunsList;
    Typeface sunfont;
    Double Dlat;
    Double Dlang;

    //to be called by the MainActivity
    public SunFragment() {
        // Required empty public constructor
    }


    private static final String KEY_LOCATION_NAME = "location_name";
    public String TAG ="SunFragment";
    public String location;//="No location name found";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve location and camera position from saved instance state.
        if (savedInstanceState != null) {
            location = savedInstanceState.getCharSequence(KEY_LOCATION_NAME).toString();
            System.out.println("OnCreate location  "+location);
           // Dlat = getArguments().getDouble("loclat");
            //Dlang = getArguments().getDouble("loclang");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_sun, container, false);
        //onSaveInstanceState(new Bundle());
        if (getArguments() != null) {
            Dlat = getArguments().getDouble("loclat");
            Dlang = getArguments().getDouble("loclang");
        } else {
            Dlat=23.1;
            Dlang=79.9864;
        }

        Log.e("Lat", Double.toString(Dlat));

我已关注this blog 来制作它,但没有传递任何值。请帮忙。

注意。这是 my earlier question 的一个更好描述和详细的问题。据我了解,需要显示更多代码。

最佳答案

在您的MainActivity中,您的sunFragment未使用。删除这部分:

/*Bundle bundle = new Bundle();
bundle.putDouble("loclat", 25.4358);
bundle.putDouble("loclang",81.8463);
Fragment SunFragment = new SunFragment();
SunFragment.setArguments(bundle);*/

您必须在 SectionsPagerAdapter 内将 bundle 设置为 fragment

case 0:
    Bundle bundle = new Bundle();
    bundle.putDouble("loclat", 25.4358);
    bundle.putDouble("loclang",81.8463);

    Fragment sunFragment = new SunFragment();
    sunFragment.setArguments(bundle);

    return sunFragment;

但是如果您需要将 bundle 设置为 MainActivity 中的 fragment。然后为此目的使用回调

关于java - 使用选项卡式 Activity 中的 bundle 将值从 Activity 传递到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190627/

相关文章:

ruby-on-rails - 用于开发的本地 Gem 路径和用于生产的远程 Git repo

Android 表单数据到 JSONObjects

java - getActivity().runOnUiThread(new Runnable(){ 上的 NullPointerException

android - 我的应用程序在 Android 4.0 上变慢了很多

java - OSGI 初学者教程已过时。有新标准吗?

Android - 将参数传递给 google play,以便在安装后打开应用程序时调用

Android如何创建运行时缩略图

JavaFX:如何让我的自定义组件使用父布局中的所有可用空间?

java - 登录后 Okhttp web 服务 session 丢失

java - 使用 Java 和 RXTX 检测 USB CDC 设备连接/断开