【发布时间】:2015-01-20 17:59:49
【问题描述】:
我在向下滚动或切换到横向模式时得到重复的项目,在发布这个新主题之前,我一直在阅读一些关于这个主题的帖子,但其中大多数解释了“if(converView) 上的“else” == null)" 我已经在我的代码中使用了,但无论如何它都在重复。
我将举一个简单的例子来说明我的问题,我的“付款”布局上有一个 ListView,顾名思义,我的 ListView 将显示我在数据库中注册的每笔付款。
基本上我使用一个扩展BaseAdapter的customerAdapter,这个适配器工作在两个完全不同的listView上,一个用于付款,另一个用于销售,我的customAdapter构造函数有3个参数
(Activity activity, ArrayList<Item_Venta_Gasto>, int tipo)
Item_Venta_Gasto 是一个类,我之前在将数据调整到我的列表视图之前将这些数据设置到该类中,然后我在我的 ArrayList 上添加每个对象以发送到我的自定义适配器。
目前我的 ListView 上有 8 条记录,其中 6 条正在完美显示,但是当我向下滚动列表视图时,剩下的 2 条正在显示为前 2 项的副本,我不确定如果你明白我在解释什么,我会上传一些截图来说明清楚。当我将手机转到横向模式时也会发生同样的情况
自定义适配器代码:
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....
Fragment在哪里设置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 的第一个视图
这是当我向下滚动 ListView 时
这是正在收集数据的表数据库
【问题讨论】:
标签: android android-listview duplicate-data android-adapterview