![](/img/trans.png)
[英]Send Data from Activity to Dialog which in Custom adapter and my custom adapter is in fragment
[英]can I send data from adapter by bundle( when click on view button from custom layout in adapter) to another fragment?
我使用 tablayout 並通過 java class 將其與片段連接起來,但我需要使用另一個片段從適配器發送數據,到片段我使用捆綁並且它從適配器獲取數據但到達片段捆綁時是 Z37A6259CC0C1DAFFAE29D9A78。 我在適配器的 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);
}
});
然后這段代碼在片段中:
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);
}
**我嘗試將此代碼放在 createdview 或 oncreate 中,但同樣的問題我需要通過捆綁或其他方式將數據從適配器發送到片段。
編輯代碼,片段事務我嘗試了很多方法:
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;
}
謝謝,**
此處提供了很好的答案
嘗試檢索值而不是捆綁
所以:
if(getArguments() != null)
shippingMeals.setName_Meal(getArguments().getString("name"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.