Issue
I was trying to use the foreach
to list my item, that is inside of my list called by checklists , but It got me an error called by Concurrent Modification. So, I found a solution like this:
//...
List<Checklist> desc = new ArrayList<>();
for (Iterator<Checklist> it = checklists.iterator(); it.hasNext(); ) {
Checklist check = it.next();
desc.add(new Checklist(check.getDescricao()));
loadRecycler(desc);
}
//...
But now, I got an other problem: my items is getting duplicated every time that I open the screen. How can I solve this problem? Follow the whole code below:
CheckListsFragment
public class ChecklistFragment extends Fragment {
@BindView(R.id.checklist_rcv_itens)
RecyclerView rvLista;
@BindView(R.id.checklist_btn_novoitem)
Button btnAddItem;
int valor;
BancoDAO bDAO;
private Checklist checklist;
private List<Checklist> checklists;
private ListChecklistAdapter listChecklistAdapter;
public ChecklistFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bDAO = new BancoDAO(getContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_checklist, container, false);
Log.i("LOG", "onCreate()");
ButterKnife.bind(this, view);
checklist = new Checklist();
checklists = bDAO.listAllItens();
//It works, but multiply my items
List<Checklist> desc = new ArrayList<>();
for (Iterator<Checklist> it = checklists.iterator(); it.hasNext(); ) {
Checklist check = it.next();
desc.add(new Checklist(check.getDescricao()));
loadRecycler(desc);
}
/* It works, but multiply my items too
for (Checklist checklist : new ArrayList<>(checklists)) {
loadRecycler(checklists);
} */
return view;
}
private int cont() {
return valor++;
}
// add 1 item to the list
@OnClick(R.id.checklist_btn_novoitem)
void addItem() {
checklists = new ArrayList<>();
checklists.add(new Checklist("Teste " + (cont() + 1) + ""));
bDAO.inserir(new Checklist("Teste " + (cont() + 1) + ""));
loadRecycler(checklists);
Helper.snackbarFast(getView(), "Item adicionado");
return;
}
private void loadRecycler(List<Checklist> lista) {
if (listChecklistAdapter == null) {
Log.i("LOG", "IF");
listChecklistAdapter = new ListChecklistAdapter(getActivity(), lista);
rvLista.setAdapter(listChecklistAdapter);
rvLista.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
rvLista.addItemDecoration(new DividerItemDecoration(getActivity(), 1));
return;
} else {
Log.i("LOG", "ELSE");
listChecklistAdapter.refreshData(lista);
}
}
Solution
Maybe if you remove this line loadRecycler(desc);
from your loop like this:
List<Checklist> desc = new ArrayList<>();
for (Iterator<Checklist> it = checklists.iterator(); it.hasNext(); ) {
Checklist check = it.next();
desc.add(new Checklist(check.getDescricao()));
}
loadRecycler(desc);
it looks like you are adding the list with each iteration.
Answered By - Barns
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.