如何在 RecyclerView 中保存更改

How to save changes in RecyclerView

我是 android 开发新手,所以我还没有掌握所有基础知识。
几天以来我一直困扰着我的问题。
我的问题如下:
我有一个 recyclerview,里面有一些物品。我创建了一个按钮来在列表中添加新项目。添加功能工作正常,我可以看到任何新项目,但是如果我滑动到第三个片段(我的应用程序中有 3 个片段,recyclerView 在左边的那个)或者如果我只是终止我的应用程序并重新启动它,每一个新的物品被销毁。
所以基本上我不知道如何在 android 应用程序中真正保存那种东西。我需要你帮我指路。
这是我的代码:

带有 recyclerview 的片段

public class CameraFragment extends Fragment  {

//matchselector reclycler view
private ArrayList<Item> items = new ArrayList<>();
private RecyclerView recyclerView;
private SnapRecyclerAdapter adapter;
// add
private View addMatch;
private AlertDialog.Builder alertDialog;
private EditText team1_add;
private View view;
private boolean add = false;
private int edit_position;
private Item newitem;


public static CameraFragment create() {
    return new CameraFragment();
}


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

    initDialog();

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    //match selector vertical recycler view
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    addMatch = (View) view.findViewById(R.id.add_match);


    /**
     * Center snapping
     */
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    final CustomLinearLayoutManager mCustomLinearLayoutManager = new CustomLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

    recyclerView.setLayoutManager(mCustomLinearLayoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new SnapRecyclerAdapter(getContext(), items);
    recyclerView.setAdapter(adapter);

    MatchList();
    adapter.notifyDataSetChanged();

    //Button addmatch click
    addMatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.add_match:
                removeView();
                add = true;
                alertDialog.setTitle("Ajout");
                team1_add.setText("");
                alertDialog.show();
                break;
        }
    }
    });

}

// dialog ajout
private void initDialog(){
    alertDialog = new AlertDialog.Builder(getContext());
    view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout_addmatch,null);
    alertDialog.setView(view);
    alertDialog.setPositiveButton("Ajouter", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(add){
                add =false;
                newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                adapter.addItem(newitem);
                dialog.dismiss();
            } else {
                items.set(edit_position, newitem);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }

        }
    });
    team1_add = (EditText)view.findViewById(R.id.team1_add);
}


private void removeView(){
    if(view.getParent()!=null) {
        ((ViewGroup) view.getParent()).removeView(view);
    }
}


public void MatchList() {
    items.add(new Item(1, "a", "b", "c","d" , "12:30" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(4, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(2, "a", "b", "c",  "d" , "12:30" , R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(3, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    adapter.notifyDataSetChanged();
}

}

SnapRecyclerAdapter:

public class SnapRecyclerAdapter extends RecyclerView.Adapter<SnapRecyclerAdapter.RecyclerViewHolder> {

private LayoutInflater layoutInflater;
private Context context;
private ArrayList<Item> items;

public SnapRecyclerAdapter(Context context, ArrayList<Item> items) {
    this.layoutInflater = LayoutInflater.from(context);
    this.context = context;
    this.items = items;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View item = layoutInflater.inflate(R.layout.item_recycler_view, parent, false);
    return new RecyclerViewHolder(item);
}

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
    Item item = items.get(position);

    holder.Logoteam1.setImageResource(item.getLogoteam1());
    holder.Logoteam2.setImageResource(item.getLogoteam2());
    holder.team1.setText(item.getTeam1());
    holder.team2.setText(item.getTeam2());
    holder.categorie.setText(item.getCategorie());
    holder.location.setText(item.getLocation());
    holder.date.setText(item.getDate());


}

public void addItem(Item newitem) {
    items.add(newitem);
    notifyItemInserted(items.size());
    notifyDataSetChanged();
}


public void removeItem(int position) {
    items.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, items.size());
}


@Override
public int getItemCount() {
    return items.size();
}

class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private ImageView Logoteam1;
    private ImageView Logoteam2;
    private TextView team1;
    private TextView team2;
    private TextView categorie;
    private TextView location;
    private TextView date;

    private RecyclerViewHolder(final View v) {
        super(v);

        Logoteam1 = (ImageView) v.findViewById(R.id.Logoteam1);
        Logoteam2 = (ImageView) v.findViewById(R.id.Logoteam2);
        team1 = (TextView) v.findViewById(R.id.team1);
        team2 = (TextView) v.findViewById(R.id.team2);
        categorie = (TextView) v.findViewById(R.id.categorie);
        location = (TextView) v.findViewById(R.id.location);
        date = (TextView) v.findViewById(R.id.date);

    }
}
}

将项目添加到回收站视图顶部的简单方法如下:

public class CameraFragment extends Fragment  {

//matchselector reclycler view
private ArrayList<Item> items = new ArrayList<>();
private RecyclerView recyclerView;
private SnapRecyclerAdapter adapter;
// add
private View addMatch;
private AlertDialog.Builder alertDialog;
private EditText team1_add;
private View view;
private boolean add = false;
private int edit_position;
private Item newitem;


public static CameraFragment create() {
    return new CameraFragment();
}


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

    initDialog();

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    //match selector vertical recycler view
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    addMatch = (View) view.findViewById(R.id.add_match);


    /**
     * Center snapping
     */
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    final CustomLinearLayoutManager mCustomLinearLayoutManager = new CustomLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

    recyclerView.setLayoutManager(mCustomLinearLayoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new SnapRecyclerAdapter(getContext(), items);
    recyclerView.setAdapter(adapter);

    MatchList();
    adapter.notifyDataSetChanged();

    //Button addmatch click
    addMatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.add_match:
        int position = 0

               newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                items.add(position,"" + newitem)
 adapter.notifyItemInserted(position);
               // removeView();
              //  add = true;
              //  alertDialog.setTitle("Ajout");
              //  team1_add.setText("");
              //  alertDialog.show();
                break;
        }
    }
    });

}

// dialog ajout
private void initDialog(){
    alertDialog = new AlertDialog.Builder(getContext());
    view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout_addmatch,null);
    alertDialog.setView(view);
    alertDialog.setPositiveButton("Ajouter", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(add){
                add =false;
                newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                adapter.addItem(newitem);
                dialog.dismiss();
            } else {
                items.set(edit_position, newitem);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }

        }
    });
    team1_add = (EditText)view.findViewById(R.id.team1_add);
}


private void removeView(){
    if(view.getParent()!=null) {
        ((ViewGroup) view.getParent()).removeView(view);
    }
}


public void MatchList() {
    items.add(new Item(1, "a", "b", "c","d" , "12:30" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(4, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(2, "a", "b", "c",  "d" , "12:30" , R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(3, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    adapter.notifyDataSetChanged();
}

}

只要加载特定片段,就会创建 Recycleview 列表中的对象。因此,每次您终止应用程序或移动到第三个片段时,都会重新加载第一个片段,并再次创建仅包含您输入的默认值的对象。只有在该对象保留在内存中之前,添加到列表中才有效,否则它会被销毁。根据您的要求,您可以使用任何存储介质,例如 Sharedpreferences(使用 Serailization)甚至 sqlite(尽管小任务不需要太多)。