1

我收到项目重复向下滚动或切换到横向模式,我已经阅读了一些关于此主题的帖子,然后发布这个新的,但大多数解释“其他”赶上“如果(converView == null)”,我已经在我的代码上,但它反正越来越重复。ListView项目重复向下滚动

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

基本上我用延伸BaseAdapter一个customerAdapter,该适配器适用于2 ListView控件完全地不同的对方,一个是支付,另一个用于销售,我customAdapter构造函数有3个参数

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

Item_Venta_Gasto是一个类,我以前在适应数据到我的列表视图我将这些数据设置到该类,然后我添加每个对象在我的ArrayList发送到我的自定义适配器。

目前我有8个记录在我的ListView其中6个正在显示完美,但是当我向下滚动列表视图时,剩下的其他2个正在显示为前两个项目的副本,我不是当然,如果你明白我在解释什么,我会上传一些截图来说明问题。同样的事情发生时,我把我的手机为横向模式

CustomAdapter代码:

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控件:

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中的第一视图

enter image description here

这是当我向下滚动的ListView

enter image description here

和这是其中数据被收集

的表数据库

enter image description here

回答

2

试试这个代码在适配器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; 
} 

希望这有助于!

+0

感谢通过回答username_AB,我想你的代码在我的getView customadapter,但我在这行上得到一个错误“LayoutInflater inflator = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);”它说“不兼容的类型:必需的android.view.LayoutInflater,找到:java.lang.Object”也许应该在调用activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)之前进行强制类型转换(LayoutInflater)? – MaToXz 2014-11-22 19:01:14

+0

是的,我的错!在那里铸造是必要的。 – AabidMulani 2014-11-22 19:04:02

+0

非常感谢你的伴侣,它完美的工作,你能帮我解释我以前的代码究竟发生了什么吗?为什么它没有按预期工作? – MaToXz 2014-11-22 19:21:05