2016-04-22 60 views
0

舊的片段。如何在數據從舊片段傳遞到新片段時獲取數據?

lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) { 
    //var intent = new Intent (this, typeof(TracksByGenres)); 
    //intent.PutStringArrayListExtra ("keys", items); 
    //StartActivity (intent); 
    FragmentTransaction fragmentTx=this.FragmentManager.BeginTransaction(); 
    TracksByGenres fragTrack=new TracksByGenres(); 
    //get our item from listview 
    fragmentTx.Replace(Resource.Id.fragmentContainer,fragTrack,items[e.Position]);  fragmentTx.AddToBackStack(null); 
    fragmentTx.Commit(); 
}; 

新片段(fragTrack)

public async override void OnActivityCreated(Bundle savedInstancesState) 
{ 
    //What can I write? I want to get value from items[e.position] from old fragment 
} 

什麼函數來獲取價值?我有搜索谷歌,但沒有結果,我希望。

+0

你想從在導航的同時傳遞數據代替傳遞數據第二個片段? – Sreeraj

+0

不,我不想要。 fragment1會將數據傳遞給第二個片段(我知道如何編寫代碼) 第二個片段會得到這個數據。我要那個。 –

回答

0

修改您的片段添加將接受數據如下

這是你從那裏你把數據第一個片段的方法。

public class FirstFragment :Fragment { 
public string mData; 
public void AddData(string data) { 
mData=data; } 
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
// inflate layout and store in variable //you can use data here // myTextView.Text=mData; } } 

這是第二個片段到您發送數據

public class SecondFragment :Fragment { 
public string mData; 
public void AddData(string data) { 
mData=data; } 
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
// inflate layout and store in variable //you can use data here // myTextView.Text=mData; } } 

現在你可以一邊做如下

SecondFragment aDifferentDetailsFrag = new SecondFragment(); 

aDifferentDetailsFrag.AddData(firstFragment.mData); 
//firstFragment is the instance of FirstFragment from where you are navigating to the secondfragment 
// Replace the fragment that is in the View fragment_container (if applicable). 

fragmentTx.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag); 
+0

我的意思是如何在新片段中獲得adifferentDetailsFrag –

+0

您的代碼將數據傳遞給新片段。 –

+0

在新的片段中,我想獲得aDifferentDetailsFrag的值。 –

相關問題