2016-04-21 27 views
0

片段1:在片段之間傳遞字符串[]。怎麼做?

lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) { 
 

 
\t \t \t \t var intent = new Intent (this, typeof(TracksByGenres)); 
 
\t \t \t \t intent.PutStringArrayListExtra ("keys", \t items); 
 
\t \t \t \t StartActivity (intent); 
 
\t \t \t }; 
 
\t \t

TracksByGenres是fragment2

Error:The best overloaded method match for `Android.Content.Intent.Intent(Android.Content.Context, System.Type)' has some invalid arguments atnew Intent (this, typeof(TracksByGenres));

Fragment2:

public async override void OnActivityCreated(Bundle savedInstancesState) 
 
\t \t { 
 
\t \t \t base.OnActivityCreated (savedInstancesState); 
 
\t \t \t paramKey = Intent.Extras.GetStringArray ("keys").ToString(); 
 

 
\t \t \t lst = View.FindViewById<ListView> (Resource.Id.lstHome); 
 

 
\t \t \t

哪裏有錯誤?

+0

是TracksByGenres繼承自Activity的類嗎? – Sreeraj

+0

編號這是片段 –

+0

檢查我的答案 – Sreeraj

回答

0
var intent = new Intent (this, typeof(TracksByGenres)); 
      intent.PutStringArrayListExtra ("keys", items); 
      StartActivity (intent); 

上面的代碼片段是創建一個新Activity的實例並啓動它。第二個參數typeof(TracksByGenres)實際上應該是typeof(A class inheriting from Actiivty (not fragment))這是導致異常的原因。請使用FragmentTransaction。這裏是關於how to manage fragments的教程。

編輯:將數據傳遞到片段的實施例,要求在評論

public class TracksByGenres :Fragment 
{ 
    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; 
    } 
} 

然後在活動,

創建第一分段,在的FrameLayout添加數據和加載

FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction(); 
TracksByGenres fragment = new TracksByGenres(); 

fragment.AddData("Xamarin is awesome"); 
// The fragment will have the ID of Resource.Id.fragment_container. 
fragmentTx.Add(Resource.Id.fragment_container, fragment); 

// Commit the transaction. 
fragmentTx.Commit(); 

更換片段與新的片段,添加數據。在這個例子中,我使用相同的片段來簡化。

TracksByGenres aDifferentDetailsFrag = new TracksByGenres(); 

aDifferentDetailsFrag.AddData("Xamarin is awesome"); 

// Replace the fragment that is in the View fragment_container (if applicable). 
fragmentTx.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag); 

// Add the transaction to the back stack. 
fragmentTx.AddToBackStack(null); 

//提交交易。 fragmentTx.Commit();

+0

從片段1獲取數據。怎麼做?我剛剛看到你的代碼正在傳遞數據 –