ListView项重复向下滚动 [英] ListView item duplicate on Scroll down
问题描述
我通过向下滚动或切换到横向模式获得重复的项目,在发布这一新主题之前,我已经阅读了有关此主题的一些帖子,但是其中大多数解释了 if(converView == null),我已经在我的代码中使用了,但是无论如何它都在重复。
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.
我将举一个简单的例子说明我的问题,我的付款布局上有一个ListView,顾名思义,我的ListView将显示我在数据库中注册的所有付款。
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.
基本上,我使用的是customerAdapter扩展BaseAdapter,此适配器在2个listView上完全不同,一个用于支付,另一个用于出售,我的customAdapter构造函数具有3个参数
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是一个类在将数据调整为列表视图之前,我将这些数据设置为该类,然后进行后续处理
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.
目前,我在ListView上有8条记录,其中6条正在完美显示,但是当我向下滚动列表视图,剩下的2个作为前2个项目的重复显示,不确定您是否了解我要解释的内容,我将上传一些屏幕截图以使其清晰。当我将手机转到横向模式时,会发生同样的事情
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代码:
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结构:
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....
设置ListView的片段:
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.
这是ListView的第一个视图
This is the First view of ListView
这是当我向下滚动ListView时
This is When I scroll down the ListView
这是收集数据的表数据库
and This is the Table Database where the data is being collected
推荐答案
在适配器中尝试以下代码 getView
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;
}
希望这会有所帮助!
这篇关于ListView项重复向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!