将Spinner值从一个片段传递到另一个片段

时间:2018-11-23 02:49:13

标签: java android android-fragments

我需要获取在第一个片段中设置的微调器的值,并将该值传递给片段2。目前,我只是试图将片段2中的一个文本字段设置为微调器的值,但稍后我需要以某种方式让片段1的微调器值与米等单位匹配,然后让片段2能够采用该单位和用户输入,然后将其转换为第二个选定的用户输入。

在片段2中,我刚刚输入了一些“错误的”伪代码以展示我要执行的操作:

主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page1));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page2));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),
                tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new
                TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });



    }

片段1

public class Page1Fragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {

        View view = inflater.inflate(R.layout.page1_fragment, container, false);


        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);

// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);



        return view;

    }

片段2

public class Page2Fragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.page2_fragment, container, false);
        fragment1Spinner = Page1Fragment.getSpinnerValue

        TextView text = (TextView) view.findViewById(R.id.textView2);
       text.setText(fragment1Spinner);

        return view;

    }
}

1 个答案:

答案 0 :(得分:0)

在微调器上添加onItemSelectedListener以获得在微调器中选择的值。将其存储在变量中,然后使用setArguments和片段的arguments方法来设置和获取值。

示例:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        // this line will get you the selected item of the spinner
        String selectedValue = parent.getItemAtPosition(position).toString(); 
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {

    }
});

用于为片段设置参数...考虑到片段名称为newInstance的方法。导航到第二个片段时进行设置。

Bundle bundle = new Bundle();
bundle.putString("selectedSpinnerItemKey", selectedSpinnerItemValue);

Fragment fragment = YourFragment.newInstance();
fragment.setArguments(bundle);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContent, fragment);
transaction.commit();

现在在第二个片段中获取参数。

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (getArguments() != null)
    {
        String itemFromFirstFragment = getArguments().getString("selectedSpinnerItemKey");
    }
}