简体   繁体   English

我可以通过捆绑从适配器发送数据(当单击适配器中自定义布局的视图按钮时)到另一个片段?

[英]can I send data from adapter by bundle( when click on view button from custom layout in adapter) to another fragment?

I use tablayout and push item tab by java class to connect it with fragment but I need to sent data from Adapter use another fragment,to fragment I use bundle and it take data from adapter but when reach to fragment bundle is null.我使用 tablayout 并通过 java class 将其与片段连接起来,但我需要使用另一个片段从适配器发送数据,到片段我使用捆绑并且它从适配器获取数据但到达片段捆绑时是 Z37A6259CC0C1DAFFAE29D9A78。 I have the following code in BindViewHolder in Adapter:我在适配器的 BindViewHolder 中有以下代码:

   holder.btn_tobuy.setOnClickListener(new View.OnClickListener() {
             FragmentShoping fragmentShoping = new FragmentShoping();
             @Override
             public void onClick(View v) {
                 String name_meal=holder.Name_Meal.getText().toString();

                 Bundle bundle = new Bundle();
                 bundle.putString("name",name_meal);
                 Log.i("BUNDLE", bundle.toString());
                 fragmentShoping.setArguments(bundle);
             }
         });

then this code in fragment:然后这段代码在片段中:

    Bundle bundle = this.getArguments();
    if(bundle!=null) {
        String i = bundle.getString("name", "un_name");
        shopingMeals.setName_Meal(i);
        shopingMeals.setPrice(10);
        shopingMeals.setCount(5);
        shopingMeals.setImg_meal(R.drawable.burger_1);
        list.add(shopingMeals);
        
    }

**I try to put this code in createdview or oncreate in fragment but the same problem I need to send data from adapter to fragment by bundle or another way. **我尝试将此代码放在 createdview 或 oncreate 中,但同样的问题我需要通过捆绑或其他方式将数据从适配器发送到片段。

edit code, fragment transaction I try a lot of way:编辑代码,片段事务我尝试了很多方法:

public FragmentShoping() {
        // Required empty public constructor
    }
    RecyclerView recycler;
    View view;
    ConstraintLayout rootlayout;
    AdapterShoping adapterShoping;
    ArrayList<ShopingMeals> list = new ArrayList<>();
    ShopingMeals shopingMeals ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_shoping, container, false);
        recycler=view.findViewById(R.id.main_recycler_shoping);
        rootlayout = view.findViewById(R.id.shoping_rootlayout);
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        getMyitem();
        adapterShoping=new AdapterShoping(getActivity().getApplicationContext(),list);
        recycler.setRotationY(180);
        recycler.setAdapter(adapterShoping);

//        meals.add(new Meals("mamoun","fff",50,R.drawable.ic_meal));
//        meals.add(new Meals("mamoun","fff",50,R.drawable.ic_meal));
//        meals.add(new Meals("mamoun","fff",50,R.drawable.ic_meal));
//        meals.add(new Meals("mamoun","fff",50,R.drawable.ic_meal));
//        recycler.setHasFixedSize(true);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Bundle bundle = this.getArguments();
//        if (bundle != null) {
//            String i = bundle.getString("name", "un_name");
//            Log.i("BUNDLE2", i);
//        }
    }
    Bundle bundle;
    String i;
    public ArrayList<ShopingMeals> getMyitem() {
        bundle =this.getArguments();
        if(bundle!=null) {
            i = bundle.getString("name", "un_name");
        }
        shopingMeals = new ShopingMeals();
            shopingMeals.setName_Meal(i);
            shopingMeals.setPrice(10);
            shopingMeals.setCount(5);
            shopingMeals.setImg_meal(R.drawable.burger_1);
            list.add(shopingMeals);
//       String a = Objects.requireNonNull(this.getActivity()).getSharedPreferences("MySharedPref", MODE_PRIVATE).getString("name", "def");
//       SharedPreferences sh = getActivity().getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
//       String s =sh.getString("name","def");
//            shopingMeals.setName_Meal(a);

            return list;

    }

thanks,**谢谢,**

Greate answer provided here 此处提供了很好的答案

Try to retrieve values instead of bundle尝试检索值而不是捆绑

So:所以:

if(getArguments() != null)
  shippingMeals.setName_Meal(getArguments().getString("name"));

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

相关问题 将数据从活动发送到对话框,其中自定义适配器和我的自定义适配器在片段中 - Send Data from Activity to Dialog which in Custom adapter and my custom adapter is in fragment 按钮上的自定义适配器刷新ListView单击 - Refreshing ListView from Custom Adapter on Button Click 适配器项从片段单击 - Adapter Item Click From Fragment 无法将数据从适配器填充到Fragment中的GridView - Can not fill data from Adapter to GridView in Fragment 如何使用包将数据从适配器共享到ViewPager中的片段 - How to use bundle to share data from adapter to a fragment in viewpager 我如何从一个片段到另一个片段的列表视图适配器进行事务 - How can i make transaction from a listview adapter from a fragment to another fragment 如何将数据从 recyclerview 适配器传递或发送到片段 - How to pass or send data from recyclerview adapter to fragment 从适配器替换时找不到片段的 id 视图 - No view found for id for fragment when replacing from Adapter 使用FragmentActivity和ViewPager中的数据,在Fragment中使用自定义ListView适配器 - Use data from FragmentActivity with ViewPager, for custom ListView adapter inside a Fragment 将数据从片段传递到自定义适配器(用于ListView) - Passing data from a fragment to a custom adapter (for use with ListView)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM