首页 > 解决方案 > 将数据发送到 Firebase 数据库后如何从片段移动到另一个片段?

问题描述

添加数据后有一个片段用于将数据发送到
我想要的firebase数据库并单击按钮发送到firebase,自动发送数据后将我发送到另一个片段

public class worker_add_fragment extends Fragment {

    EditText phone, name;
    Button addWorker;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.add_worker, container, false);
         phone = view.findViewById(R.id.phone);
         name = view.findViewById(R.id.name);
         addWorker = view.findViewById(R.id.addBtn);

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference table_employee = database.getReference("Employees");

        addWorker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ProgressDialog dialog = new ProgressDialog(getActivity());
                dialog.setMessage("انتظر!");
                dialog.show();

                if (name.length()==0){
                    dialog.dismiss();
                    name.setError("ادخل اسمك");
                }else if (phone.length()==0){
                    dialog.dismiss();
                    phone.setError("ادحل رقم تليفونك");
                }else{

                    table_employee.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            //check if already phone exist
                            if (snapshot.child(phone.getText().toString()).exists()){
                                dialog.dismiss();
                                Toast.makeText(getActivity(), "انت مسجل من قبل", Toast.LENGTH_LONG).show();
                            }else{
                                dialog.dismiss();
                                Worker worker = new Worker(name.getText().toString());
                                table_employee.child(phone.getText().toString()).setValue(worker);
                                Toast.makeText(getActivity(), "تم التسجيل بنجاح", Toast.LENGTH_LONG).show();

                                //are the  code that send me to another fragment write here?
                                getActivity().finish();


                            }
                        }
                        @Override
                        public void onCancelled(@NonNull DatabaseError error) { }
                    });
                }
            }
        });
        return view;
    }
}

..................................................... ................................
任何帮助都可以得到赞赏。谢谢

标签: androidandroid-studiofragment

解决方案


您可以在您希望片段显示的 xml 文件中定义片段标记,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<fragment
    android:id="@+id/fragment_content_1"
    android:name="com.example.fragmentexample.Fragment_1"
    android:layout_width="0dip"
    android:layout_weight="0.50"
    android:layout_height="fill_parent" >
</fragment>

</LinearLayout>

然后你可以在你的主要活动中创建新功能,它膨胀片段意味着它在它的 xml 文件中

public void replaceFragment(Fragment fragment){
getSupportFragmentManager()
     .beginTransaction()
     .replace(R.id.fragment_content_1,fragment)
     .commit();
}

在您评论此行的地方调用此函数 //are the code that send me to another fragment write here?


推荐阅读