如何將一些數據轉移到另一個Fragment
同樣,extras
爲intents
?如何將一些數據轉移到另一個片段?
回答
使用Bundle
。下面是一個例子:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Bundle已經爲許多數據類型提供了方法。請參閱您的Fragment
this
然後,檢索的數據(例如,在onCreate()
法):
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
嗨,你的答案thanx,但我們需要實現任何東西??像可序列化或Parcelable? –
我正在傳遞一個類的對象的arraylist .. –
不,你不需要實現任何類。 – Gene
getArguments()將返回null,因爲 「它沒有得到任何東西」
試試這個代碼來處理這種情況
if(getArguments()!=null)
{
int myInt = getArguments().getInt(key, defaultValue);
}
喜thanx您的答案將其分解,但我們需要實現什麼?像序列化或Parcelable? –
我傳遞一個類的對象的ArrayList .. –
你確定嗎?因爲我已經實現Serializable/Parcelable當我經過使用意圖片段和活動之間複雜的數據...... –
只是爲了擴展前真誠的答案 - 它可以幫助某人。如果您回報null
,把它onCreate()
方法,而不是你的片段的構造函數:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int index = getArguments().getInt("index");
}
要更延續之前的回答,就像是ANKIT說,對於複雜的對象,你需要實現Serializable接口。例如,對於簡單對象:
public class MyClass implements Serializable {
private static final long serialVersionUID = -2163051469151804394L;
private int id;
private String created;
}
在你FromFragment:
Bundle args = new Bundle();
args.putSerializable(TAG_MY_CLASS, myClass);
Fragment toFragment = new ToFragment();
toFragment.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.body, toFragment, TAG_TO_FRAGMENT)
.addToBackStack(TAG_TO_FRAGMENT).commit();
在ToFragment
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
MyClass myClass = (MyClass) args
.getSerializable(TAG_MY_CLASS);
使用片段傳遞數據的完整代碼片段
Fragment fragment = new Fragment(); // replace your custom fragment class
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
bundle.putString("key","value"); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(viewID,fragment);
fragmentTransaction.commit();
在定製的片斷類
Bundle mBundle = new Bundle();
mBundle = getArguments();
mBundle.getString(key); // key must be same which was given in first fragment
你輸入片段
public class SecondFragment extends Fragment {
EditText etext;
Button btn;
String etex;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.secondfragment, container, false);
etext = (EditText) v.findViewById(R.id.editText4);
btn = (Button) v.findViewById(R.id.button);
btn.setOnClickListener(mClickListener);
return v;
}
View.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
etex = etext.getText().toString();
FragmentTransaction transection = getFragmentManager().beginTransaction();
Viewfragment mfragment = new Viewfragment();
//using Bundle to send data
Bundle bundle = new Bundle();
bundle.putString("textbox", etex);
mfragment.setArguments(bundle); //data being send to SecondFragment
transection.replace(R.id.frame, mfragment);
transection.isAddToBackStackAllowed();
transection.addToBackStack(null);
transection.commit();
}
};
}
視圖片段
public class Viewfragment extends Fragment {
TextView txtv;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.viewfrag,container,false);
txtv = (TextView) v.findViewById(R.id.textView4);
Bundle bundle=getArguments();
txtv.setText(String.valueOf(bundle.getString("textbox")));
return v;
}
}
First Fragment Sending String To Next Fragment
public class MainActivity extends AppCompatActivity {
private Button Add;
private EditText edt;
FragmentManager fragmentManager;
FragClass1 fragClass1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Add= (Button) findViewById(R.id.BtnNext);
edt= (EditText) findViewById(R.id.editText);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragClass1=new FragClass1();
Bundle bundle=new Bundle();
fragmentManager=getSupportFragmentManager();
fragClass1.setArguments(bundle);
bundle.putString("hello",edt.getText().toString());
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.activity_main,fragClass1,"");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
Next Fragment to fetch the string.
public class FragClass1 extends Fragment {
EditText showFrag1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.lay_frag1,null);
showFrag1= (EditText) view.findViewById(R.id.edtText);
Bundle bundle=getArguments();
String a=getArguments().getString("hello");//Use This or The Below Commented Code
showFrag1.setText(a);
//showFrag1.setText(String.valueOf(bundle.getString("hello")));
return view;
}
}
I used Frame Layout easy to use.
Don't Forget to Add Background color or else fragment will overlap.
This is for First Fragment.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/colorPrimary"
tools:context="com.example.sumedh.fragmentpractice1.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/BtnNext"/>
</FrameLayout>
Xml for Next Fragment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtText"/>
</LinearLayout>
解釋你的答案?不由分說的代碼不會做太大的幫助 – Coder
我有一個流,從而它可以被理解編寫的代碼.....傳遞數據從主要活動於FragClass1與使用捆綁的。 –
- 1. 將數據從一個片段發送到另一個片段
- 2. 如何從一個活動的片段發送一些數據到另一個活動的另一個片段
- 3. 如何從片段移動到另一個片段
- 4. Android:使用BroadcastReceiver將數據從一個片段傳遞到另一個片段
- 5. 從活動轉到片段並轉到另一個片段
- 6. 將數據從一個表格轉移到另一個表格?
- 7. 如何將數據從輸入字段轉移到另一個js文件
- 8. 用php將一些數據從一個數據庫轉移到另一個數據庫?
- 9. 如何將數組的一部分轉移到另一個? Javascript
- 10. 如何將數據從一個表移動到另一個表#
- 11. 如何將表從一個SQL Server數據庫轉移到另一個?
- 12. 如何將JSONArray從一個片段傳遞到另一個片段
- 13. 在Android中將另一個片段滑過另一個片段
- 14. 片段傳輸數據到另一個片段
- 15. 如何通過活動將額外數據從一個片段傳輸到另一個片段
- 16. 將表從一個數據庫轉移到另一個數據庫?
- 17. 從片段到另一個片段
- 18. 如何使用Haskell將某些數據從一個數據庫移動到另一個數據庫?
- 19. 傳遞數據從一個片段到另一個 - SharedPreferences
- 20. 如何將表從一個數據庫移動到另一個數據庫
- 21. 將表格數據移動到另一個表格字段
- 22. Android片段 - 從一個視圖移動到另一個視圖?
- 23. 從一個片段移動到另一個
- 24. 如何將行數據從1個JTable移動到另一個?
- 25. 如何將控件從一個活動轉移到android studio中另一個活動的片段?
- 26. 將參數從一個視圖轉移到另一個視圖?
- 27. 從一個活動(數據庫)轉移到另一個活動
- 28. 如何將這些爆炸數據移動到另一個表格中? Laravel 5.3
- 29. 如何從一個片段移動到新的片段?
- 30. 將一個片段替換爲另一個片段
我嘗試[這裏]回答這個問題@(HTTP: //stackoverflow.com/a/27626004/4249919)。我希望工程。 – ozhanli