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