Tengo una vista donde listo un resultado que obtengo de mi base de datos y pasa que cuando se pasa de cantidad de elementos y empieza a funcionar el scroll empieza a pasar cosas extrañas con mi lista de elementos cosas como:
1 - se duplican registros (aveces si a veces no) según utilice el scroll. Como podrán ver en las imágenes el mismo registro aparece 2 veces y en la base de datos lo tengo una sola ves, incluso si hago mucho movimiento de scroll de momento es otro el elemento que se duplica algo aleatorio.
Les comparto los códigos donde creo se puede apreciar como cargo el ListView:
Activity:
// Construct AlertDialog AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// load view
LayoutInflater inflaterTemp = this.getLayoutInflater();
final View dialogView = inflaterTemp.inflate(R.layout.dialog_last_dte, null);
dialogBuilder.setView(dialogView);
// Open connection local (Ticket)
DSTicket DSTicket = new DSTicket(getApplicationContext());
List<ModelTicket> tickets = DSTicket.listSend(DateHelper.getDate("yyyy-MM-dd"));
String total = DSTicket.sum("2");
String totalCanceled = DSTicket.sumCanceled("2");
DSTicket.close();
// load totals in view
TextView ammount = dialogView.findViewById(R.id.ammount);
if(total.equals("null")){
total = "0.00";
} else {
total = String.valueOf(CalculatorHelper.ajustAmmount(Float.valueOf(total)));
}
ammount.setText("Total ventas: " + total + " $");
TextView ammountCanceled = dialogView.findViewById(R.id.ammountCanceled);
if(totalCanceled.equals("null")){
totalCanceled = "0.00";
} else {
totalCanceled = String.valueOf(CalculatorHelper.ajustAmmount(Float.valueOf(totalCanceled)));
}
ammountCanceled.setText("Total anulado: " + totalCanceled + " $");
ListView listViewTickets = dialogView.findViewById(R.id.list);
Adapter adapter = new AdapterTicket(getApplicationContext(), DashboardActivity.this, (ArrayList<ModelTicket>) tickets);
listViewTickets.setAdapter((ListAdapter) adapter);
Adapter:
public class AdapterTicket extends ArrayAdapter<ModelTicket>{
private ArrayList<ModelTicket> m_dataSet;
Context m_context;
Activity m_activity;
private static class ViewHolder {
TextView MsgRow;
TextView Ticket;
Button ButtonReprint;
Button ButtonCancel;
}
public AdapterTicket(Context context, Activity activity, ArrayList<ModelTicket> data) {
super(context, R.layout.dialog_last_dte_ticket, data);
this.m_dataSet = data;
this.m_context = context;
this.m_activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final ModelTicket ticketModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.dialog_last_dte_ticket, null);
viewHolder.Ticket = convertView.findViewById(R.id.ticket);
viewHolder.ButtonReprint = convertView.findViewById(R.id.reprint);
viewHolder.ButtonCancel = convertView.findViewById(R.id.cancel);
viewHolder.MsgRow = convertView.findViewById(R.id.msgRow);
viewHolder.Ticket.setText("$"+ ticketModel.getM_mnttotal() +" \n " + ticketModel.getM_houremis());
if(ticketModel.getM_folio().equals(ticketModel.getM_nota_folioref())){
viewHolder.ButtonReprint.setVisibility(View.INVISIBLE);
viewHolder.ButtonCancel.setVisibility(View.INVISIBLE);
viewHolder.MsgRow.setVisibility(View.VISIBLE);
}
convertView.setTag(viewHolder);
// capture click in row
/*
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(Constants.TAG, "Fila: " + ticketModel.getM_folio());
}
});
*/
// capture click in button reprint
viewHolder.ButtonReprint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// generate ticket
Intent printTicket = new Intent(m_activity, PrintTicketActivity.class);
printTicket.putExtra("ticketId", String.valueOf(ticketModel.getM_id()));
printTicket.putExtra("dirpath", "reprint");
m_context.startActivity(printTicket);
}
});
// capture click in cancel
viewHolder.ButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// generate nota de crédito
Intent cancelTicket = new Intent(m_activity, CancelTicketActivity.class);
cancelTicket.putExtra("ticketId", String.valueOf(ticketModel.getM_id()));
// Open connection local (Ticket)
DSTicket DSTicket = new DSTicket(m_context);
String[] data = new String[9];
// save cancel ticket
data[0] = String.valueOf(getCaf("61").getM_id()); // NOTA_CAFID
data[1] = getCaf("61").getM_documenttype(); // NOTA_TIPODTE
data[2] = getCaf("61").getM_rng_a(); // NOTA_FOLIO
data[3] = ticketModel.getM_folio(); // NOTA_FOLIOREF
data[4] = ticketModel.getM_tipodte(); // NOTA_TIPODTEREF
data[5] = ticketModel.getM_nota_cafidref(); // NOTA_CAFIDREF
data[6] = "1"; // ANULA_RAZONREF [1, DEV D LA BOLETA],[2, COR GIRO DEL RECP], [3, DEV DE MERC]
data[7] = DateHelper.getDate("yyyy-MM-dd HH:mm:ss"); // ANULA_RAZONREF
data[8] = "ANULA BOLETA"; // ANULA_RAZONREF
boolean resultTicket = DSTicket.cancel(ticketModel.getM_id(), data);
DSTicket.close();
// increment +1 rang used
incrementCaf("61");
// activity for generate PDF
m_context.startActivity(cancelTicket);
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
/*
* Get data to CAF from DB LOCAL
**/
public ModelCaf getCaf(String documenttype){
// get caf local database
DSCaf DSCaf = new DSCaf(m_context);
ModelCaf resultCaf = DSCaf.get(documenttype);
DSCaf.close();
return resultCaf;
}
/*
* Increment CAF +1
**/
public boolean incrementCaf(String documenttype){
// get caf local database
DSCaf DSCaf = new DSCaf(m_context);
boolean resultCaf = DSCaf.increment(documenttype);
DSCaf.close();
return resultCaf;
}
}
Gracias de antemano espero puedan ayudarme.