Làm thế nào để chuyển một số dữ liệu khác Fragment
tương tự như vậy nó đã được thực hiện với extras
cho intents
?Làm cách nào để chuyển một số dữ liệu sang một Phân đoạn khác?
Trả lời
Sử dụng Bundle
. Dưới đây là ví dụ:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Gói đã đặt phương thức cho nhiều loại dữ liệu. Xem this
Sau đó, trong Fragment
của bạn, lấy dữ liệu (ví dụ như trong onCreate()
phương pháp) với:
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
Hi thanx cho câu trả lời của bạn nhưng chúng ta cần phải thực hiện bất cứ điều gì ?? như Serializable hoặc Parcelable ?? –
tôi đang chuyển danh sách mảng của đối tượng của một lớp .. –
Không, bạn không cần triển khai bất kỳ lớp nào. – Gene
getArguments() được trở về null vì "nó không nhận được bất cứ điều gì"
Hãy thử mã này để xử lý tình huống này
if(getArguments()!=null)
{
int myInt = getArguments().getInt(key, defaultValue);
}
Hi thanx cho câu trả lời của bạn nhưng chúng ta có cần triển khai bất kỳ thứ gì không ?? Serializable hoặc Parcelable ?? –
Tôi đang chuyển danh sách mảng của một đối tượng của một lớp .. –
bạn có chắc chắn không? Bởi vì tôi phải triển khai Serializable/Parcelable khi tôi chuyển dữ liệu phức tạp giữa một đoạn và hoạt động bằng cách sử dụng mục đích ...... –
Chỉ cần kéo dài trước những câu trả lời thú vị - nó có thể giúp ai đó. Nếu lợi nhuận của bạn null
, đặt nó để onCreate()
phương pháp chứ không phải xây dựng các mảnh của bạn:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int index = getArguments().getInt("index");
}
Để mở rộng câu trả lời trước nhiều hơn, như Ankit đã nói, các đối tượng phức tạp mà bạn cần phải thực hiện Serializable. Ví dụ, đối với các đối tượng đơn giản:
public class MyClass implements Serializable {
private static final long serialVersionUID = -2163051469151804394L;
private int id;
private String created;
}
Trong bạn 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();
trong ToFragment của bạn:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
MyClass myClass = (MyClass) args
.getSerializable(TAG_MY_CLASS);
mã hoàn chỉnh của dữ liệu qua sử dụng đoạn để mảnh
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();
Trong lớp đoạn tùy chỉnh
Bundle mBundle = new Bundle();
mBundle = getArguments();
mBundle.getString(key); // key must be same which was given in first fragment
đoạn đầu vào của bạn
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();
}
};
}
xem đoạn bạn
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>
Giải thích câu trả lời của bạn? mã mà không có bất kỳ lời giải thích sẽ không giúp đỡ nhiều – Coder
tôi đã viết mã trong một dòng chảy để nó có thể được hiểu ..... Truyền dữ liệu từ hoạt động chính để FragClass1 với việc sử dụng bó. –
- 1. Cách chuyển dữ liệu từ hoạt động sang phân đoạn
- 2. Cách chuyển dữ liệu từ một đoạn đến hộp thoạiFragment
- 3. chuyển dữ liệu nhanh từ tệp sang một số StringIO
- 4. làm thế nào để chuyển dữ liệu thêm từ một mảnh sang người khác qua hoạt động
- 5. Cách chuyển dữ liệu giữa hoạt động sang phân đoạn bằng ý định trong Android
- 6. Làm cách nào để sao chép dữ liệu nhị phân sang một chuỗi
- 7. Làm cách nào để sao chép dữ liệu nhị phân từ luồng này sang luồng khác?
- 8. Làm cách nào để di chuyển dữ liệu postgresql sang một thư mục khác trên Ubuntu trên Amazon EC2?
- 9. Làm cách nào để chuyển dữ liệu từ hoạt động này sang hoạt động khác?
- 10. Chuyển dữ liệu nhị phân làm đối số trong bash
- 11. Làm cách nào để di chuyển thư mục 'Đoạn mã của tôi' của Visual Studio sang một ổ đĩa khác?
- 12. Làm cách nào để chuyển 'dòng đối số' của một chức năng PowerShell sang một chức năng khác?
- 13. Chuyển tất cả dữ liệu từ một thuê bao sang một thuê bao khác
- 14. Chuyển từ đoạn bản đồ sang phân đoạn khác và quay lại
- 15. Làm cách nào để di chuyển Sơ đồ cơ sở dữ liệu SQL Server sang cơ sở dữ liệu khác?
- 16. Cách chuyển một số dữ liệu từ máy chủ sang thiết bị android
- 17. Làm cách nào để chuyển đổi dữ liệu thập lục phân của dữ liệu sang dữ liệu nhị phân trong PHP?
- 18. Di chuyển một dữ liệu danh sách mảng sang một danh sách mảng khác trong C#
- 19. Làm cách nào để chuyển thông tin giữa các phân đoạn bảng phân cảnh?
- 20. Cách di chuyển dữ liệu và cài đặt từ cơ sở dữ liệu này sang cơ sở dữ liệu khác?
- 21. Làm cách nào để di chuyển dữ liệu của Cassandra từ một không gian khóa sang một không gian phím khác?
- 22. Sao chép bảng từ một cơ sở dữ liệu sang một cơ sở dữ liệu khác
- 23. Có cách nào để chuyển tiếp một tài liệu PDF sang một tài liệu PDF khác bằng iTextSharp không?
- 24. Làm cách nào để tôi có thể sử dụng lại một phân đoạn đoạn Android trên các phần khác nhau?
- 25. Làm cách nào để chuyển dữ liệu từ Perl sang Java?
- 26. Làm cách nào để chuyển tham số sang Ext.data.Store?
- 27. Chuyển dữ liệu từ cơ sở dữ liệu này sang cơ sở dữ liệu khác
- 28. Làm thế nào để chuyển đổi một số thập phân thành một int trong C#?
- 29. Trong Perl, làm cách nào để chuyển một hàm làm đối số của một hàm khác?
- 30. Làm thế nào để sao chép dữ liệu từ một bảng sang một bảng mới khác trong MySQL?
Tôi cố gắng trả lời câu hỏi này @ [ở đây] (http: //stackoverflow.com/a/27626004/4249919). Tôi hy vọng t hoạt động. – ozhanli