My recyclerview is repeating and overlapping the results.
cursor=Os.getCursorPecas();intiRow=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_ID);intiId=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_ID_PECA);intiName=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_NAME);intiValor=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_VALOR);intiRef=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_REF);intiQtde=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_QTDE);intiUnidade=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_UNIDADE);intiIdCat=cursor.getColumnIndex(DataBaseHandler.KEY_PECAS_OS_CATEGORIA);intiCat=cursor.getColumnIndex(DataBaseHandler.KEY_PRODUTOS_CATEGORIA_NAME);if(cursor.moveToFirst()){do{//Log.v(LOG,"id carregado "+cursor.getInt(iRow));
PecasList lista = new PecasList(
cursor.getInt(iRow),
cursor.getInt(iId),
cursor.getString(iName),
null,
cursor.getString(iRef),
cursor.getDouble(iValor),
0.0,
null,
cursor.getInt(iQtde),
cursor.getString(iCat),
cursor.getInt(iIdCat),
cursor.getString(iUnidade));
pecasLists.add(lista);
}
while (cursor.moveToNext());
}
recyclerView.setAdapter(new ProdutosOsListAdapter(getContext(), pecasLists, ProdutosFragment.this));
My Adapter:
public class ProdutosOsListAdapter extends RecyclerView.Adapter<ProdutosOsListAdapter.PecasViewHolder> {
private List<PecasList> lista;
Context context;
ProdutosFragment fragment;
static int[] color;
public ProdutosOsListAdapter(Context context, List<PecasList> lista, ProdutosFragment fragment) {
this.context = context;
this.lista = lista;
this.fragment = fragment;
color = AppContext.getContext().getResources().getIntArray(R.array.rainbow);
}
@Override
public PecasViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_pecas_os, parent, false);
return new PecasViewHolder(v);
}
@Override
public void onBindViewHolder(final PecasViewHolder holder, int position) {
final PecasList itens = lista.get(position);
holder.nome.setText(itens.getName());
int qtde = itens.getQtde();
holder.valor.setText(qtde + " x " + Funcoes.moeda(itens.getValor()) + " = " + Funcoes.moeda(qtde * itens.getValor()));
try {
Drawable drawable = holder.shape.getBackground();
holder.shape.setText(itens.getName().substring(0, 1).trim());
int posit = AlfabetoEnum.valueOf(itens.getName().trim().substring(0, 1)).getValorLetra();
drawable.setColorFilter(color[posit], PorterDuff.Mode.SRC_IN);
} catch (Exception ignored) {
}
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment.novoProduto(true, itens.getId() + "", (itens.getIdPec() == 0) ? null : itens.getIdPec() + "", null);
}
});
}
@Override
public int getItemCount() {
return lista.size();
}
class PecasViewHolder extends RecyclerView.ViewHolder {
public TextView nome, valor, shape;
public ImageButton btn;
RelativeLayout row;
PecasViewHolder(View view) {
super(view);
row = (RelativeLayout) view.findViewById(R.id.rowPeca);
nome = (TextView) view.findViewById(R.id.nomePeca);
valor = (TextView) view.findViewById(R.id.valorPeca);
nome.setEllipsize(TextUtils.TruncateAt.END);
nome.setSingleLine(true);
shape = (TextView) view.findViewById(R.id.shapeLetra);
btn = (ImageButton) view.findViewById(R.id.btn);
}
}
}