[英]Send data from one fragment to another fragment in xamarin.android
[英]Change fragment from ListActivity in Android Xamarin
我正在嘗試從listActivity對話框中選擇項目后更改當前片段
這是我的listActivity.cs類:
[Activity( Theme = "@android:style/Theme.Dialog" , Label ="Number list") ]
class NumberListActivity : ListActivity
{
string[] data;
ArrayAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.data = Intent.GetStringArrayExtra("list_numbers");
adapter = new ArrayAdapter(this, Resource.Layout.specific_contact_number_list_layout, data);
ListAdapter = adapter;
}
protected override void OnListItemClick(ListView l, View v, int position, long id)
{
base.OnListItemClick(l, v, position, id);
//Intent callFragment = new Intent(this, typeof(barMenu));
//callFragment.PutExtra("callNumber", data[position]);
//StartActivity(callFragment);
ISharedPreferences preferences = GetSharedPreferences(SignInActivity.userSessionPref, FileCreationMode.Private);
String UserNamevalueSession = preferences.GetString("UserNamevalue", "");
String UserphonevalueSession = preferences.GetString("Userphonevalue", "");
CallService.phone.Connect(data[position], UserphonevalueSession);
DateTime now = DateTime.Now;
SqliteDB db = new SqliteDB();
db.Insert(data[position], now.ToString());
var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();
Finish();
}
}
這里的一切工作都很好,我只需要在單擊項目后更改片段
我嘗試了這個
var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();
但它給我
09-27 12:57:00.247 E / AndroidRuntime(11446):java.lang.IllegalArgumentException:找不到片段MainActivity {4b067018#0 id = 0x7f080079}的ID 0x7f080079(TurkeyanaCall.TurkeyanaCall:id / conent_fragment)的視圖
我的specific_contact_number_list_layout.xmal代碼
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/number_list"
android:textColor="#37c837"
android:textSize="20dip"
android:textStyle="italic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft ="20dp"
android:padding ="10dp"
/>
這是包含我要更改其名稱為bar_menu.xmal的Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/maintActivity_drawerlayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearView">
<fragment
class="TwilioClientTest.Android.MainMenuFragment"
android:id="@+id/conent_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/maintActivity_navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/shadow"
app:menu="@menu/left_menus"
app:theme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.v4.widget.DrawerLayout>
這里有些錯誤。
首先,您嘗試將活動用作片段。
var frag = new MainActivity();
其次,默認情況下, ListActivity
為您設置包含ListView
的內容視圖。 因此,沒有conent_fragment
視圖。
如果您只想顯示MainActivity,則可以替換
var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.conent_fragment, frag).Commit();
同
StartActivity(typeof(MainActivity));
如果您確實想使用片段,則必須進行一些更改。 如果是這種情況,我可以更新我的答案。
編輯片段
創建一個名為number_list
的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/number_list_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</FrameLayout>
這里重要的事情是添加一個ID為android:id="@android:id/list"
的ListView
。 這將替代默認設置。
然后在您的OnCreate
添加SetContentView(Resource.Layout.number_list);
然后當你想顯示片段
var frag = new MainActivity();
FragmentManager.BeginTransaction().Replace(Resource.Id.number_list_content, frag).Commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.