2013-08-03 70 views
1

我能夠從Activity中獲取FragmentActivity中的字符串。但是,如何將我的數據從FragmentActivity傳遞到碎片?儘管我能夠在FragmentActivity中獲取數據。將FragmentActivity中的字符串傳遞給片段

(我不想將數據存儲在SharedPreferences。)

public class IndividualDetailsActivity extends IndividualBasePager { 

    ViewPager mPager; 
    IndividualDetailsBaseAdapter mAdapter; 
    TitlePageIndicator mIndicator; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.individual_pager); 
     mAdapter = new IndividualDetailsBaseAdapter(getSupportFragmentManager()); 
     mPager = (ViewPager) findViewById(R.id.pager); 
     mPager.setOffscreenPageLimit(4); 
     mPager.setAdapter(mAdapter); 
     mIndicator = (TitlePageIndicator) findViewById(R.id.indicator); 
     mIndicator.setViewPager(mPager); 
     mIndicator.setCurrentItem(1); 
     Intent i = getIntent(); 
     getSupportActionBar().setTitle(i.getStringExtra("name")); 
    } 
} 

public class IndividualBasePager extends SherlockFragmentActivity { 
    IndividualDetailsBaseAdapter mAdapter; 
    ViewPager mPager; 
    PageIndicator mIndicator; 

    @Override 
    protected void onCreate(Bundle arg0) { 
     super.onCreate(arg0); 
     requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); 
     ActionBar actionbar = getSupportActionBar(); 
     actionbar.setDisplayHomeAsUpEnabled(true); 
     setSupportProgressBarIndeterminateVisibility(true); 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == android.R.id.home) { 
      finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 


public class IndividualDetailsBaseAdapter extends FragmentPagerAdapter { 
    protected static final String[] CONTENT = new String[] { "Comments", 
      "Details", "Screens", "Tutorial" }; 

    private int mCount = CONTENT.length; 

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

    @Override 
    public Fragment getItem(int position) { 
     Log.e("PAGE", "Position " + position); 
     switch (position) { 
     case 0: 
      return new CommentsFragment(); 
     case 1: 
      return new DetailsFragment(); 
     case 2: 
      return new CommentsFragment(); 
     case 3: 
      return new CommentsFragment(); 
     } 
     return null; 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position) { 
     return IndividualDetailsBaseAdapter.CONTENT[position % CONTENT.length]; 
    } 

    public void setCount(int count) { 
     if (count > 0 && count <= 10) { 
      mCount = count; 
      notifyDataSetChanged(); 
     } 
    } 
} 

回答

0

的文檔建議您使用片段的父活動。您不應該使用片段來分割通信,以便您可以在其他地方重複使用該片段(如有必要並保持其可重複使用)。話雖如此,我會建議使用放在捆綁上的Extras。

你把這樣的事情:

savedInstanceState.putString("myString", myStingVariable); 

,你可以讓他們像這樣:

myStingVariable = savedInstanceState.getString("myString"); 

你也將要檢查的值不爲空。

請注意,不同類型的變量有不同的getter和setter。

希望這會有所幫助。

1

在您的片段中,您可以通過調用以下方法getActivity()來獲得FragmentActivity。並在您的FragmentActivity創建一個方法,如getValue()這將返回您所需的數據。

0

你可以在你的片段中創建一個setter方法如下。從你的活動中,只需調用該方法即可。

public class IndividualDetailsBaseAdapter extends FragmentPagerAdapter { 
protected static final String[] CONTENT = new String[] { "Comments", 
     "Details", "Screens", "Tutorial" }; 

private int mCount = CONTENT.length; 

private String name; 
public void setName(String name) 
{ 
    this.name=name; 
} 


public IndividualDetailsBaseAdapter(FragmentManager fm) { 
    super(fm); 
} 
... // Rest of your stuff. 
} 

你可以從你的活動如下調用的setName方法,

mAdapter = new IndividualDetailsBaseAdapter(getSupportFragmentManager()); 
mAdapter.setName(getIntent().getStringExtra("name"));