【问题标题】:add new item of RecyclerView from other fragment从其他片段添加新的 RecyclerView 项
【发布时间】:2020-07-07 08:38:43
【问题描述】:

我有 buyfragment 在 RecyclerView 上添加了项目列表。我想从另一个片段(sellfragment)通过按钮调用(startselling)添加新项目..所以我只需要lstDressAdd()方法或onClick(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));

}}

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

    }
});

请帮帮我,这是我的大学项目,两天后的截止日期

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • notifyItemInserted(int position),,我怎么知道哪个位置
    【解决方案2】:

    试试这个代码。

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

    [编辑]

    【讨论】:

    • adapter.lstDress 不可用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多