从其他片段中添加新的RecyclerView项目

时间:2020-03-26 16:34:29

标签: android android-studio android-fragments android-recyclerview fragment

我有buyfragment个在RecyclerView上添加了项目列表。我想从另一个片段(sellfragment)中通过按钮calles(startselling)添加新项。.因此,我只需要用于lstDressAdd()方法或onClick({{1 }})按钮

这是(startselling):

buyfragment

这是(附体):

 public class buyFragment extends Fragment {

public List<Dresses> lstDress;
 RecyclerView myrv;
RecyclerViewAdapter myAdapter;


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


lstDress = new ArrayList<>();

//DRESSES DATA
lstDress.add(new Dresses("flora V-neck Drawstring A-line dress", "Bersheka", "new", 150.0, R.drawable.dress22));
lstDress.add(new Dresses("Button front flounce waist floral dress", "Pull&Bear", "new", 200.0, R.drawable.dress2));
lstDress.add(new Dresses("Shirred waist split hem Flower print dress", "Stradivarius", "new", 199.0, R.drawable.dress1));
lstDress.add(new Dresses("flora print square neck split hem midi dress", "MANGO", "new", 175.0, R.drawable.e));
lstDress.add(new Dresses("confetti heart surplice neck knot sleeve dress", "ZARA", "new", 168.0, R.drawable.dress22));
lstDress.add(new Dresses("flora V-neck Drawstring A-line dress", "ZARA", "new", 100.0, R.drawable.dress2));
lstDress.add(new Dresses("Shirred waist split hem Flower print dress", "Stradivuse", "new", 199.0, R.drawable.dress1));



myrv = (RecyclerView) view.findViewById(R.id.recyclerviwe_id);
myAdapter = (new RecyclerViewAdapter(getContext(), lstDress));
myrv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
myrv.setAdapter(myAdapter);

return view;
 }


  public void lstDressAdd(String dress_title, String brand, String use, Double price) {

  lstDress.add(new Dresses(dress_title, brand, use, price, R.drawable.dress1));

}}

请帮助我这是我的大学项目,两天后到期

2 个答案:

答案 0 :(得分:0)

基本上,您需要向列表中添加一个新项目,然后通知您的recyclerView。 n.lstDressAdd(dress_title.getText().toString(),text,Use,Double.parseDouble(price.getText().toString())); n.myAdapter.notifyItemInserted()

答案 1 :(得分:0)

尝试使用此代码。

public void lstDressAdd(String dress_title, String brand, String use, Double price) {

      lstDress.add(new Dresses(dress_title, brand, use, price, R.drawable.dress1));
      myAdapter.notifyDataSetChanged(); 
      myAdapter = (new RecyclerViewAdapter(getContext(), lstDress)); 
}

[编辑]