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

问题描述 投票:0回答:2

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

的正确代码

这是(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));

}}

和这(分词):

     startselling=(Button)view.findViewById(R.id.button3) ;
     startselling.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String text = brands.getSelectedItem().toString();

        String Use;
        if(newradio.getText().toString()=="New dress")
            Use="New";
        else
            Use="Used";
        String d=dress_title.getText().toString();

        Double p=Double.parseDouble(price.getText().toString());
        if(d==null &&p==null&&Use==null&&text==null){
            Toast.makeText(getContext(), "you need to complete the dress Info", Toast.LENGTH_SHORT).show();

        }else{
        buyFragment n=new buyFragment();
        n.lstDressAdd(dress_title.getText().toString(),text,Use,Double.parseDouble(price.getText().toString()));
        Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();}

    }
});

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

android android-studio android-fragments android-recyclerview fragment
2个回答
0
投票

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


0
投票

尝试将此方法添加到buyfragment类中

public void refreshAdapter() {
 if(lstDress != null) {
    adapter.lstDress = lstDress;
    adapter.notifyDataSetChanged();

   }
}

并在sellfragment的n.lstDressAdd中添加以下语句

   n.refreshAdapter();
© www.soinside.com 2019 - 2024. All rights reserved.