android - 向下滚动时 ListView 项目重复

标签 android android-listview duplicate-data android-adapterview

我在向下滚动或切换到横向模式时得到了重复的项目,在发布这个新主题之前我已经阅读了一些关于这个主题的帖子,但是他们中的大多数解释了“if(converView) == null)”,我的代码中已经有了,但无论如何它都在重复。

我将举一个简单的例子来说明我的问题,我的“付款”布局上有一个 ListView,顾名思义,我的 ListView 将显示我在数据库中注册的每笔付款。

基本上我使用一个扩展 BaseAdapter 的 customerAdapter,这个适配器在 2 个完全不同的 listView 上工作,一个用于支付,另一个用于销售,我的 customAdapter 构造函数有 3 个参数

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

Item_Venta_Gasto 是一个类,我之前在将数据调整到我的 ListView 之前将这些数据设置到该类中,随后我将每个对象添加到我的 ArrayList 中以发送到我的自定义适配器。

目前我的 ListView 上有 8 条记录,其中 6 条显示完美,但是当我向下滚动 ListView 时,剩下的 2 条显示为前 2 项的副本,我不确定如果你明白我在解释什么,我会上传一些截图来说明。当我 Handlebars 机调到横向模式时,同样的事情发生了

自定义适配器代码:

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 结构:

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....

设置 ListView 的 fragment :

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;

}

这里有几个屏幕截图可以向您展示问题。

这是ListView的第一个 View

enter image description here

这是当我向下滚动 ListView 时

enter image description here

这是收集数据的表数据库

enter image description here

最佳答案

在您的适配器 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;
}

希望这对您有所帮助!

关于android - 向下滚动时 ListView 项目重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080859/

相关文章:

java - 带有外部库的 Robolectric NoClassDefFoundError

java - 如何获取Listview中单选按钮的值点击Android中的按钮

java - ListView 项目未显示在主 Activity 上

android - 在 Android 自定义适配器中设置 Textview 文本的问题

javascript - 无法复制多维数组

sql - 删除带有警告的重复项

sql - 如果我停止长时间运行的查询,它会回滚吗?

android - 在Android上使用Phonegap,电子邮件数组为contacts.find返回null

android - ConcurrentModificationException 即使在 android ArrayList 中使用 Iterator

android - 通过 BroadcastReceiver 替代 FileObserver?