ListView item duplicate on Scroll down

5.2k views Asked by At

I'm getting items duplicate on scroll down or switch to landscape mode, I've been reading some post about this subject before posting this new one, but the most of them explain the "else" catch on "if(converView == null)" which I already have on my code, but It anyways is getting duplicating.

I'm going to give a simple example of what is my problem, I have a ListView on my "Payments" layout, as the name says, my ListView will show every payment I had register on my database.

Basically I use a customerAdapter which extends BaseAdapter, this adapter works on 2 listView completly different each other, one is for payment and the other one for sells, my customAdapter constructor have 3 parameters

(Activity activity, ArrayList<Item_Venta_Gasto>, int tipo)

Item_Venta_Gasto is a class where I previously before adapting the data to my listview I set those data into that class, and subsequenly I add each object on my ArrayList to be send to my custom Adapter.

Currently I have 8 records on my ListView which 6 of those are being show perfectly, but when I scroll down the list view, the others 2 remaining are being show as the duplicate of the first 2 items, I'm not sure if you understand what I'm explaning, I'll upload some screenshot to make it clear. The same thing happen when I turn my phone to landscape mode

CustomAdapter code:

public class VentaGastoListAdapter extends BaseAdapter{

private Activity activity;
ArrayList<Item_Venta_Gasto> arrayitms;
int tipo;

public VentaGastoListAdapter(Activity activity, ArrayList<Item_Venta_Gasto> arrayitms, int tipo) {
    this.activity = activity;
    this.arrayitms = arrayitms;
    this.tipo = tipo;
}

public View getView(int position, View convertView, ViewGroup parent) {
    Fila1 view = null;
    LayoutInflater inflator = activity.getLayoutInflater();
    Item_Venta_Gasto itm;
    if(convertView==null)
    {
        switch (tipo){
            case 0:
                view = new Fila1();
                //Creo objeto item y lo obtengo del array
                itm = arrayitms.get(position);
                convertView = inflator.inflate(R.layout.gasto_item, null);
                //Fecha
                view.fecha = (TextView) convertView.findViewById(R.id.rowDate);
                //Seteo en el campo titulo el nombre correspondiente obtenido del objeto
                view.fecha.setText(itm.getFecha());
                //descipcion
                view.descripcion = (TextView) convertView.findViewById(R.id.rowDescription);
                //Seteo la descripcion
                view.descripcion.setText(itm.getConcepto()+" - "+itm.getDescripcion());
                //saldo
                view.saldo = (TextView)convertView.findViewById(R.id.rowPrice);
                //seteo el saldo
                view.saldo.setText(itm.getSaldo()+"BsF");
                convertView.setTag(view);
                break;
            case 1:
                view = new Fila1();
                //Creo objeto item y lo obtengo del array
                itm = arrayitms.get(position);
                convertView = inflator.inflate(R.layout.gasto_item, null);
                //Fecha
                view.fecha = (TextView) convertView.findViewById(R.id.rowDate);
                //Seteo en el campo titulo el nombre correspondiente obtenido del objeto
                view.fecha.setText(itm.getFecha());
                //descipcion
                view.descripcion = (TextView) convertView.findViewById(R.id.rowDescription);
                //Seteo la descripcion
                view.descripcion.setText(itm.getCliente()+" "+itm.getCantidad()+" "+itm.getProducto());
                //saldo
                view.saldo = (TextView)convertView.findViewById(R.id.rowPrice);
                //seteo el saldo
                view.saldo.setText(itm.getSaldo()+"BsF");
                convertView.setTag(view);
                break;
        }

    }
    else
    {
        view = (Fila1) convertView.getTag();
    }
    return convertView;
}

Item_Venta_Gasto Structure:

public class Item_Venta_Gasto {
int id;
String fecha;
String concepto;
String descripcion;
String saldo;
String producto;
String cliente;
String cantidad;
Context context;

public Item_Venta_Gasto(int id, String fecha, String producto, String cliente, String cantidad, String saldo, Context context) {
    this.context = context;
    this.id = id;
    this.fecha = fecha;
    this.producto = producto;
    this.cliente = cliente;
    this.cantidad = cantidad;
    this.saldo = saldo;
    this.concepto = null;
    this.descripcion = null;


}

public Item_Venta_Gasto(int id, String fecha, String concepto, String descripcion, String saldo) {

    this.id = id;
    this.fecha = fecha;
    this.concepto = concepto;
    this.descripcion = descripcion;
    this.saldo = saldo;
    this.producto = null;
    this.cliente = null;
    this.cantidad = null;
}

Getter and Setter methods....

Fragment where is set the ListView:

public class GastoFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

ListView lista = (ListView)  view.findViewById(R.id.gastoListView);

Cursor cursor = db.cargarCursorOrderBy("gasto",new String[]{"*"},"fecha");
    ArrayList<Item_Venta_Gasto> listaGasto = new ArrayList<Item_Venta_Gasto>();
    if(cursor.moveToFirst()){
        for(int i = 0; i < cursor.getCount(); i++){
            listaGasto.add(new Item_Venta_Gasto(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)));
            cursor.moveToNext();
        }
    }
    VentaGastoListAdapter adapter = new VentaGastoListAdapter(getActivity(),listaGasto,0);
    lista.setAdapter(adapter);
    return view;

}

Here's a couple of screenshots to show you the problem.

This is the First view of ListView

enter image description here

This is When I scroll down the ListView

enter image description here

and This is the Table Database where the data is being collected

enter image description here

1

There are 1 answers

5
AabidMulani On BEST ANSWER

Try this code in your adaptor getView

public View getView(int position, View convertView, ViewGroup parent) {
    Fila1 view = null;
    Item_Venta_Gasto itm;
    if(convertView==null) {
                view = new Fila1();
                LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.gasto_item, null);
                view.fecha = (TextView) convertView.findViewById(R.id.rowDate);
                view.descripcion = (TextView) convertView.findViewById(R.id.rowDescription);
                view.saldo = (TextView)convertView.findViewById(R.id.rowPrice);
                convertView.setTag(view);
    }else{
        view = (Fila1) convertView.getTag();
    }
    itm = arrayitms.get(position);
    view.fecha.setText(itm.getFecha());
    view.saldo.setText(itm.getSaldo()+"BsF");
    switch (tipo){
        case 0:
            view.descripcion.setText(itm.getConcepto()+" - "+itm.getDescripcion());
            break;
        case 1:
            view.descripcion.setText(itm.getCliente()+" "+itm.getCantidad()+" "+itm.getProducto());
            break;
    }
    return convertView;
}

Hope this helps!