简体   繁体   English

将数据从一个片段传递到另一个片段,从 FirebaseRecyclerAdapter

[英]Passing data from one fragment to another fragment from FirebaseRecyclerAdapter

I am new to this.我是新来的。 I know there are a lot of questions about this but I can't solve this problem.我知道有很多关于此的问题,但我无法解决这个问题。 I want to pass data from one fragment to another fragment through firebaserecycleradapter我想通过 firebaserecycleradapter 将数据从一个片段传递到另一个片段

This is my first Fragment这是我的第一个片段

private  FirebaseDatabase database;
private  DatabaseReference mReference;
private  RecyclerView recyclerView_food;
private  RecyclerView.LayoutManager layoutManager;
private  LinearLayout nonVegBtn;
private  HomeItem adapterHome;
private ProgressDialog progressDialog;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu_home, container, false);
        progressDialog.setMessage("Loading...");
        database=FirebaseDatabase.getInstance();
        recyclerView_food=(RecyclerView) view.findViewById(R.id.homeFoodRecycleView);
        recyclerView_food.setLayoutManager(new LinearLayoutManager(getContext()));
        Query query = mReference = FirebaseDatabase.getInstance().getReference("Categoria");
        FirebaseRecyclerOptions<Categoria> options = new FirebaseRecyclerOptions.Builder<Categoria>().
                setQuery(query,Categoria.class).build();
        adapterHome = new HomeItem(options);
        recyclerView_food.setAdapter(adapterHome);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        adapterHome.startListening();
    }
    @Override
    public void onStop() {
        super.onStop();
        adapterHome.stopListening();
    }
}

This is the FirebaseRecyclerAdapter from which I want to pass the data to the second fragment, I used the Bundle method but it doesn't work这是我想将数据从中传递到第二个片段的 FirebaseRecyclerAdapter,我使用了 Bundle 方法,但它不起作用

public class HomeItem extends FirebaseRecyclerAdapter<Categoria,HomeItem.viewHolder>{

    public HomeItem(@NonNull FirebaseRecyclerOptions<Categoria> options) {
        super(options);
    }


    @Override
    protected void onBindViewHolder(@NonNull viewHolder holder, int position, @NonNull Categoria model) {

        String getID=getRef(position).getKey();


        holder.texFood.setText(model.getName());
        Glide.with(holder.imageFood.getContext()).load(model.getImage()).placeholder(R.drawable.non_veg).into(holder.imageFood);
        holder.itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Bundle bundle = new Bundle();
                bundle.putString("categoryID",getID);
                AllFoodItem newsFragment = new AllFoodItem();
                newsFragment.setArguments(bundle);

               AppCompatActivity appCompatActivity =(AppCompatActivity)holder.itemButton.getContext();
               appCompatActivity.getSupportFragmentManager().beginTransaction().replace(R.id.frameItemLeyaut,
                       new AllFoodItem()).addToBackStack(null).commit();

            }
        });

    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item,parent,false);
        return new viewHolder(v);
    }

    public class viewHolder extends RecyclerView.ViewHolder{
        public TextView texFood;
        public ImageView imageFood;
        public LinearLayout itemButton;

        public viewHolder(@NonNull View itemView) {
            super(itemView);

            texFood=itemView.findViewById(R.id.nonVegtxt);
            imageFood=itemView.findViewById(R.id.foodIMG);
            itemButton=itemView.findViewById(R.id.nonVegBtn);
        }
    }

}

And this is the second fragment where I want to get the data.这是我想要获取数据的第二个片段。 Please help me what method to use请帮助我使用什么方法

public class AllFoodItem extends Fragment {

    RecyclerView mRecyclerView;
    FirebaseDatabase database;
    DatabaseReference mReference;
    private FoodViewHolder adapterFood;
    String getIdFood ="";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.all_food_item, container, false);

        database=FirebaseDatabase.getInstance();
        mReference = FirebaseDatabase.getInstance().getReference("food");

        mRecyclerView=view.findViewById(R.id.nonVegRecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2,GridLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        Bundle bundle = this.getArguments();
        if (bundle!=null){
            getIdFood=bundle.getString("categoryID");
        }

        Query query = mReference = FirebaseDatabase.getInstance().getReference("food");
        FirebaseRecyclerOptions<Food> options = new FirebaseRecyclerOptions.Builder<Food>().
                setQuery(query.orderByChild("MenuId").equalTo(getIdFood),Food.class).build();
        adapterFood = new FoodViewHolder(options);
        mRecyclerView.setAdapter(adapterFood);

        return view;
    }

    @Override
    public void onStop() {
        super.onStop();
        adapterFood.stopListening();
    }
    @Override
    public void onStart() {
        super.onStart();
        adapterFood.startListening();
    }
}

When you launch your new fragment, you create an instance of it ( newsFragment ) and set its arguments here当您启动新片段时,您会创建它的一个实例 ( newsFragment ) 并在此处设置它的 arguments

Bundle bundle = new Bundle();
bundle.putString("categoryID",getID);
AllFoodItem newsFragment = new AllFoodItem();
newsFragment.setArguments(bundle);

then when you go on to start the fragment you completely ignore the fragment instance you just created, and create a new instance here (with no bundle arguments set)然后当你 go 开始片段时,你完全忽略了你刚刚创建的片段实例,并在此处创建一个新实例(没有捆绑包 arguments 集)

appCompatActivity.
    getSupportFragmentManager().
    beginTransaction().
    replace(R.id.frameItemLeyaut, new AllFoodItem()). // <- here
    addToBackStack(null).
    commit();

Instead of calling new AllFoodItem() there you should pass in the fragment instance you already created and set the arguments on, like this而不是在那里调用new AllFoodItem()你应该传入你已经创建的片段实例并将 arguments 设置为 on,就像这样

appCompatActivity.
    getSupportFragmentManager().
    beginTransaction().
    replace(R.id.frameItemLeyaut, newsFragment). // <- here
    addToBackStack(null).
    commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 数据检索并将其从片段传递到新活动不起作用 - Firebase data retrieve and passing it from fragment to new activity not working 如何将数据从 Activity 传递到 Fragment KOTLIN? - How to pass data from Activity to Fragment KOTLIN? Flutter 作为 stream 将数据从一个屏幕传递到另一个屏幕 - Flutter passing data from one screen to another as a stream 如何从片段中的 Firebase 实时数据库中检索数据? - How to Retrieve Data from Firebase Realtime Database in Fragment? CloudFormation 将变量从一个堆栈传递到另一个堆栈 - CloudFormation passing variables from one stack to another 如何通过点击通知打开片段? - How to open fragment from the click of the notification? 在另一个片段中使用 Firestore 回调 - Using Firestore callback in another fragment 需要帮助将这段 Google 登录和注册代码从片段转换为片段的 viewModel,以便我可以遵循 MVVM - Need help to convert this chunk of Google Sign in and registration code from fragment to viewModel for fragment so I can follow the MVVM 如何从特定片段的活动中禁用 onDestroy 方法? - How to disable an onDestroy method from activity in a specific fragment? 如何在片段 class 的文本视图中显示来自 firebase 的值 - how to display a value from firebase in a text view in a fragment class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM