Reputation: 1538
In my class of adapter I'm having problems getView method (). When I print the positions of the elements that I want to list, always missing the last but repeated the first. For example, I have an array of my class of RowItem (custom) class with 7 elements. The positions I get is:
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
System.out.println("Position : "+position);
return null
}
Position : 0
Position : 1
Position : 2
Position : 3
Position : 4
Position : 5
.....
However, if the method getCount print receipt
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
System.out.println("Count "+getCount());
return null;
}
Count : 7
Count : 7
Count : 7
Count : 7
Count : 7
Count : 7
This causes, that my last element is always like the first. On my list I have a button that when you click, modify a value in the cell to which the button belongs. But this error, it modified only visually the first element and the last. My class :
@SuppressLint("InflateParams")
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
private ArrayList<RowItem> items;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
this.items = new ArrayList<RowItem>();
this.items.addAll(items);
}
/*private view holder class*/
static class ViewHolder {
TextView txtNombre;
TextView txtTicket;
TextView txtAsiento;
TextView txtOrden;
TextView txtNumero;
TextView txtMensaje;
TextView txtAdicionales;
TextView txtOtros;
TextView txtCategoria;
Button btn;
}
@Override
public int getCount() {
return items.size();
}
@Override
public RowItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
As I call this class from my activity
CustomListViewAdapter adapter;
public List<RowItem> rowItems = new ArrayList<RowItem>();
rowItems = nueva.getInscripcionsByRut(ValidacionMultiple.this, rut_inscripcion);
adapter = new CustomListViewAdapter(ValidacionMultiple.this,R.layout.lista_validacion_multiple, rowItems );
listView.setAdapter(adapter);
Method getInscripcionsByRut
public List<RowItem> getInscripcionsByRut(Context context, String rut){
inscripcionValida = new Inscripcions();
List<RowItem> rowItems = new ArrayList<RowItem>();
RowItem item;
bdTickets = new TicketsBaseDatos(context, Config.NAME_DATABASE, null, 1);
SQLiteDatabase db = bdTickets.getWritableDatabase();
SessionManager manager = new SessionManager();
String codigoEvento = manager.getValue(context, "codigoEvento");
String[] args = new String[] {rut,codigoEvento};
String codigo_checkin = manager.getValue(context, "checkin");
Cursor c = db.rawQuery("SELECT * FROM Inscripcion WHERE rut=? AND codigo_evento=?",args);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
item = new RowItem();
inscripcionValida.setId(c.getInt(0));
inscripcionValida.setHash(c.getString(1));
inscripcionValida.setCodigo_evento(c.getString(2));
inscripcionValida.setTicket(c.getString(3));
inscripcionValida.setNombre(c.getString(4));
inscripcionValida.setInscripcion_id(c.getString(5));
inscripcionValida.setValidado(c.getInt(6));
inscripcionValida.setSincronizado(c.getInt(7));
inscripcionValida.setNumero(c.getString(8));
inscripcionValida.setAsiento(c.getString(9));
inscripcionValida.setAdicionales(c.getString(10));
inscripcionValida.setOtros(c.getString(11));
inscripcionValida.setCategoria(c.getString(12));
inscripcionValida.setRut(c.getString(14));
inscripcionValida.setTalla(c.getString(15));
inscripcionValida.setNombreResponsable(c.getString(17));
inscripcionValida.setFechaValidacion(c.getString(16));
item.setAsiento(inscripcionValida.getAsiento());
item.setNumero(inscripcionValida.getNumero());
item.setHash(inscripcionValida.getHash());
item.setNombre(inscripcionValida.getNombre());
item.setTicket(inscripcionValida.getTicket());
item.setOtros(inscripcionValida.getOtros());
item.setAdicionales(inscripcionValida.getAdicionales());
if(manager.getValue(context,"checkin") != null){
item.setValidado(checkinValidado(context,inscripcionValida.getInscripcion_id(),codigo_checkin));
}else{
item.setValidado(inscripcionValida.getValidado());
}
item.setId_inscripcion(Integer.parseInt(inscripcionValida.getInscripcion_id()));
rowItems.add(item);
}
c.close();
db.close();
return rowItems;
}
It is also sending data from my DB to my List to display in the listview.
I got my method getView like this:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
System.out.println(getCount());
ViewHolder holder;
RowItem rowItem = getItem(position);
if (convertView == null) {
LayoutInflater mInflater = LayoutInflater.from(getContext());
convertView = mInflater.inflate(R.layout.lista_validacion_multiple,null);
holder = new ViewHolder();
holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
holder.btn = (Button)convertView.findViewById(R.id.button1);
holder.txtMensaje = (TextView)convertView.findViewById(R.id.txtMensaje);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtNombre.setText(Html.fromHtml("Nombre: <b>"+rowItem.getNombre()+"</b>"));
if(rowItem.getValidado()==1){
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
holder.txtMensaje.setText("E-ticket ya validado");
}else{
holder.btn.setTag(position);
holder.btn.setOnClickListener(new View.OnClickListener() {
//More code...
});
}
convertView.setTag(holder);
return convertView;
}
What happens here is that if I click on the button of the first item or the last item, you get the same data and do not correspond.
Upvotes: 0
Views: 585
Reputation: 5375
Just add holder.btn.setOnClickListener(null);
in the if condition.
if(rowItem.getValidado()==1){
holder.btn.setOnClickListener(null);
}else{
holder.btn.setOnClickListener(new View.OnClickListener() {
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Button b = (Button)v.findViewById(R.id.button1);
b.setTag(position);
//More elements.
});
}
The inconsistent behaviour you see is because of view recycling in listview. Your views for list items are recycled and reused. Say you list has got 100 items so 100 different views will not be created. It will only create unique views approximately equal to the number of items visible in the list. So if a item disappears from the list, its view is not destroyed, it is given to the other items.
public View getView(int position, View convertView, ViewGroup parent);
here convertView is the view that gets recycled
Now in your code you assigned a onClickListener to one view. For the 7th item, the earlier would have been recycled for which onClickListener has already been set. So just remove that onClickListener on whichever row you don't want the listener to be present
Upvotes: 1