在片段之间传递字符串[]。怎么做?
Passing string[] between fragment. How to do that?
片段 1:
lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {
var intent = new Intent (this, typeof(TracksByGenres));
intent.PutStringArrayListExtra ("keys", items);
StartActivity (intent);
};
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));
片段 2:
public async override void OnActivityCreated(Bundle savedInstancesState)
{
base.OnActivityCreated (savedInstancesState);
paramKey = Intent.Extras.GetStringArray ("keys").ToString();
lst = View.FindViewById<ListView> (Resource.Id.lstHome);
哪里不对?
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.
的教程
编辑:将数据传递给 Fragment 的示例,在评论中询问
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;
}
}
然后在Activity,
创建第一个片段,在 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();
片段 1:
lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {
var intent = new Intent (this, typeof(TracksByGenres));
intent.PutStringArrayListExtra ("keys", items);
StartActivity (intent);
};
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));
片段 2:
public async override void OnActivityCreated(Bundle savedInstancesState)
{
base.OnActivityCreated (savedInstancesState);
paramKey = Intent.Extras.GetStringArray ("keys").ToString();
lst = View.FindViewById<ListView> (Resource.Id.lstHome);
哪里不对?
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.
编辑:将数据传递给 Fragment 的示例,在评论中询问
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;
}
}
然后在Activity,
创建第一个片段,在 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();