简体   繁体   English

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

[英]add new item of RecyclerView from other fragment

I have buyfragment that have List of items added on RecyclerView.我有在 RecyclerView 上添加了项目列表的buyfragment I want from another fragment ( sellfragment ) to add new item by a button calles( startselling ) ..So I just need a right code for lstDressAdd() method or for onClick ( startselling )button我从另一个片段(想sellfragment )通过按钮卡列斯(添加新项startselling )..所以我只需要一个正确的代码lstDressAdd()方法或的onClick( startselling )按钮

this is the ( buyfragment ):这是( 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));

}}

and this the (sellfragment):这是(sellfragment):

     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();}

    }
});

pleses help me this is my college project and the due date after two days请帮助我这是我的大学项目和两天后的截止日期

Basically you need to add a new item to the list and notify your recyclerView after that.基本上,您需要向列表中添加一个新项目,然后通知您的 recyclerView。 n.lstDressAdd(dress_title.getText().toString(),text,Use,Double.parseDouble(price.getText().toString())); n.myAdapter.notifyItemInserted()

try to this code.试试这个代码。

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)); 
}

[Edit] [编辑]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM