我收到項目重複向下滾動或切換到橫向模式,我已經閱讀了一些關於此主題的帖子,然後發佈這個新的,但大多數解釋「其他」趕上「如果(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中的第一視圖
這是當我向下滾動的ListView
和這是其中數據被收集
的表數據庫
感謝通過回答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
是的,我的錯!在那裏鑄造是必要的。 – AabidMulani 2014-11-22 19:04:02
非常感謝你的伴侶,它完美的工作,你能幫我解釋我以前的代碼究竟發生了什麼嗎?爲什麼它沒有按預期工作? – MaToXz 2014-11-22 19:21:05