2017-10-12 8 views
-2

J'ai un problème pour ajouter plus de 1 ligne (item) dans mon RecyclerView, l'élément écrase le précédent quand j'appuie sur le bouton, mais si je crée un Liste <> avec les données codées en dur sur onCreate, cela fonctionne en ajoutant plus de 1 ligne dans le RecyclerView. Suivez le code:RecyclerView - Je ne peux pas ajouter plus de 1 ligne

ListChecklistAdapter

public class ListChecklistAdapter extends RecyclerView.Adapter<ListChecklistAdapter.ListChecklistViewHolder> { 
    private List<Checklist> mChecklist; 
    private Context mCtx; 

    public ListChecklistAdapter(Context ctx, List<Checklist> checklists) { 
     mCtx = ctx; 
     mChecklist = checklists; 
    } 

    @Override 
    public ListChecklistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(mCtx).inflate(R.layout.item_checklist, parent, false); 
     return new ListChecklistViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ListChecklistViewHolder holder, int position) { 
     holder.cbItem.setText(mChecklist.get(position).getDescricao()); 
     holder.cbItem.setChecked(mChecklist.get(position).isCheckado()); 
    } 

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

    public class ListChecklistViewHolder extends RecyclerView.ViewHolder { 
     @BindView(R.id.item_checkbox) 
     CheckBox cbItem; 

     public ListChecklistViewHolder(View view) { 
      super(view); 
      ButterKnife.bind(this, view); 
     } 
    } 

    public void refreshData(List<Checklist> item) { 
     this.mChecklist.clear(); 
     this.mChecklist.addAll(item); 
     notifyDataSetChanged(); 
    } 
} 

item_checklist

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <CheckBox 
     android:id="@+id/item_checkbox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:paddingLeft="6dp" 
     android:text=" " 
     tools:text="Exemplo de item de lista" /> 

</RelativeLayout> 

MainActivity

public class MainActivity extends AppCompatActivity { 
    @BindView(R.id.rcv_lista) 
    RecyclerView rvLista; 
    int valor; 
    private List<Checklist> lista; 
    private ListChecklistAdapter listChecklistAdapter; 
    @BindView(R.id.btn_add_item) 
    Button btnAdd; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 

    } 

    private int cont() { 
     return valor++; 
    } 

    public void novoItem(View view) { 
     lista = new ArrayList<>(); 
     lista.add(new Checklist("Item " + (cont() + 1), true)); 
     loadRecycler(lista); 
    } 

    private void loadRecycler(List<Checklist> lista) { 
     if (listChecklistAdapter == null) { 
      Log.i("LOG", "IF"); 

      listChecklistAdapter = new ListChecklistAdapter(this, lista); 

      rvLista.setAdapter(listChecklistAdapter); 
      rvLista.setLayoutManager(new LinearLayoutManager(getApplicationContext())); 
      rvLista.addItemDecoration(new DividerItemDecoration(this, 1)); 
      return; 
     } else { 
      Log.i("LOG", "ELSE"); 
      listChecklistAdapter.refreshData(lista); 
      listChecklistAdapter.onAttachedToRecyclerView(rvLista); 
     } 
    } 
} 

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="16dp" 
    tools:context="teste.com.br.recyclercomcheckbox.MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rcv_lista" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btn_add_item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_marginTop="5dp" 
     android:background="@android:color/transparent" 
     android:onClick="novoItem" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:text="+ novo item" 
     android:textColor="#009688" /> 

</LinearLayout> 
+1

Déplacer lista = new ArrayList <>(); hors de votre méthode novoItem. Initialiser une seule fois – nomag

Répondre

0

enlèverait juste cette ligne this.mChecklist.clear(); de votre méthode refreshData().

public void refreshData(List<Checklist> item) { 
    this.mChecklist.addAll(item); 
    notifyDataSetChanged(); 
} 
+0

* le problème dans votre code est que chaque fois que vous définissez un nouvel adaptateur * non, seulement la première fois - lisez le code ... * vous avez utilisé listChecklistAdapter.notifyDataSetChanged(); * il s'appelle novoItem-> loadRecycler-> listChecklistAdapter.refreshData-> notifyDataSetChanged – Selvin

+2

problème est la création de la nouvelle liste à chaque fois – nomag

+0

@Selvin avez-vous vérifié dans son 'public actualid refreshData (Liste article)' il est d'abord effacement de sa liste entière que d'ajouter de nouveaux –

0

@Nilesh Rathod a résolu le problème! Je viens de supprimer this.mChecklist.clear(); du ListCheckAdapter.java > refreshData() Merci à tous pour votre aide!