【问题标题】:How to move from fragment to another after send data to Firebase database?将数据发送到 Firebase 数据库后如何从片段移动到另一个片段?
【发布时间】:2021-01-26 02:56:41
【问题描述】:

有用于向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;
    }
}

.................................................. ................................
任何帮助都将不胜感激。谢谢

【问题讨论】:

    标签: android android-studio fragment


    【解决方案1】:

    您可以在您希望片段显示的 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?

    【讨论】:

    • sry 我不明白(什么是 placeholder1?)当调用函数时什么是参数?
    • 它是片段的 id,即“fragment_content_1”,请参见 标记 id
    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2018-04-16
    相关资源
    最近更新 更多