2016-06-27 7 views
1

私は2つのタブのフラグメントにアクティビティを持ち、バンドルを使用してメインクラスからフラグメントに変数を送信しようとしています。メインアクティビティからフラグメントへのバンドルデータの送信

しかし、私は情報を受け取ることができず、唯一nullになっています。

ありがとうございました!

public class DetailedInfo extends AppCompatActivity { 

    private SectionsPagerAdapter mSectionsPagerAdapter; 
    private ViewPager mViewPager; 


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

     /////INFO, I WANT TO SEND 
     InfoFragment fragment = new InfoFragment(); 
     Bundle args = new Bundle(); 
     args.putString("key", "text i want to send"); 
     fragment.setArguments(args); 
     //// 


     //TABS setup 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     // Create the adapter that will return a fragment for each of the two 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_detailed_info, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     return super.onOptionsItemSelected(item); 

    } 


    public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        return PicturesFragment.newInstance(position + 1); 
       case 1: 
        return InfoFragment.newInstance(position + 1); 


      } 
      return null; 

     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "Pictures"; 
       case 1: 
        return "Info"; 

      } 
      return null; 
     } 
    } 

    public static class PicturesFragment extends Fragment { 

     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public static PicturesFragment newInstance(int sectionNumber) { 
      PicturesFragment fragment = new PicturesFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View detailedpictures = inflater.inflate(R.layout.fragment_detailed_pictures, container, false); 

      return detailedpictures; 
     } 
    } 

    public static class InfoFragment extends Fragment { 

     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public static InfoFragment newInstance(int sectionNumber) { 
      InfoFragment fragment = new InfoFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View detailedinfo = inflater.inflate(R.layout.fragment_detailed_info, container, false); 

      ////INFO, I WANT TO RECEIVE 
      Bundle bundle = getArguments(); 
      String customtext = bundle.getString("key"); 
      TextView detailtext= (TextView)detailedinfo.findViewById(R.id.info); 
      detailtext.setText(customtext); 
      //// 

      return detailedinfo; 
     } 
    } 
} 
+0

私は思います最後にバンドルを上書きしています。新しいバンドルを作成するのではなく、上に作成したバンドルを使用することもできます。 –

答えて

0

あなたは、私は次のことを示唆している、あなたの断片上sectionNumberを使用していないので:

public static class InfoFragment extends Fragment { 

public static InfoFragment newInstance(String value) { 
    InfoFragment fragment = new InfoFragment(); 
    Bundle args = new Bundle(); 
    args.putInt("key", value); 
    fragment.setArguments(args); 
    return fragment; 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View detailedinfo = inflater.inflate(R.layout.fragment_detailed_info, container, false); 

    ////INFO, I WANT TO RECEIVE 
    Bundle bundle = getArguments(); 
    String customtext = bundle.getString("key"); 
    TextView detailtext= (TextView)detailedinfo.findViewById(R.id.info); 
    detailtext.setText(customtext); 
    //// 

    return detailedinfo; 
} 

}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

private InfoFragment infoFragment; 
private PictureFragment picFragment; 

public SectionsPagerAdapter(FragmentManager fm, InfoFragment infoFrg, PictureFragment picFrg) { 
    super(fm); 
    this.infoFragment = infoFrg; 
    this.picFragment = picFrg; 
} 

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
     case 0: 
      return infoFragment; 
     case 1: 
      return picFragment; 
    } 
    return null; 
} 

し、最終的に:

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

    /////INFO, I WANT TO SEND 
    InfoFragment infoFragment = InfoFragment.newInstance("text I want to send"); 
    //// 
    PictureFragment picFrament = PictureFragment.newInstance(0); 

    //TABS setup 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    // Create the adapter that will return a fragment for each of the two 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), infoFragment, picFragment); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 
} 
+1

ありがとう、それは問題を解決し、解決しました! – Mikk

関連する問題