2017-07-26 99 views
0

我是Android開發人員中的新成員,因此我還沒有掌握所有基礎知識。
自我幾天以來,我一直在困擾我的問題。
我的問題如下:
我得到了裏面的一些項目recyclerview。我創建了一個按鈕在列表中添加新項目。添加函數工作正常,我可以看到任何新項目,但如果我滑動到第三個片段(我的應用程序中有3個片段,並且recyclerView位於左側),或者如果我簡單地殺掉我的應用程序並重新啓動它,物品被銷燬。
所以基本上我不知道如何在android應用程序中真正保存這種東西。我需要你的幫助來向我展示。
這裏是我的代碼:如何在RecyclerView中保存更改

片段與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); 

    } 
} 
} 
+1

我不關注,你想如何在onDestroy之後保存它們?你知道這些物體是保存在記憶中的嗎?糾正我,如果我錯了,但我沒有看到任何持久的方式來保存數據(db,sharedprefs,存儲等)。 –

+0

我認爲你需要以某種方式堅持你的數據。您有幾個選項 - 共享首選項,文件系統上的文件和一些數據庫。解釋它們將是相當長的任務,所以我建議您閱讀「數據存儲指南」 - https://developer.android.com/guide/topics/data/data-storage.html – stan0

回答

0

對象列表中的R每當加載特定片段時,都會創建ecycleview。因此,每次你殺死應用程序或移動到你的第三個片段時,第一個片段將被重新加載,並且對象被再次創建,只包含你所放置的默認值。添加到列表僅在該對象保留在內存中時纔有效,否則將被銷燬。根據您的要求,您可以使用任何存儲介質,如Sharedpreferences(使用Serailization)或甚至sqlite(儘管對於小任務來說不需要太多)。

1

一個簡單的方法將項目添加到回收視圖頂部如下:

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

}