Fustigador
Fustigador

Reputation: 6459

Adapter returning same item twice

I have a ListView holding some items. It shows the result of a search in database, where two items meets the criteria of the select sentence.

When i do the search the first time, it returns twice the same item. But, when I click the search button again, this time it is done ok, showing both items. What could I do to get the items properly the first time?

Adapter:

public class ListaExpedientesAdapter extends BaseAdapter {

private ArrayList<BeanListaExpedientes> listaExpedientes;
private LayoutInflater inflater=null;
private Context c;

public ListaExpedientesAdapter(Context c, ArrayList<BeanListaExpedientes> lista){
    this.listaExpedientes=lista;
    inflater=LayoutInflater.from(c);
    this.c=c;
}

@Override
public int getCount() {
    return listaExpedientes.size();
}

@Override
public Object getItem(int position) {

    return listaExpedientes.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    ViewHolder holder;
    if(convertView==null){
        convertView=inflater.inflate(R.layout.search_exp_adapter_layout, null);
        holder=new ViewHolder();
        holder.numexp=(TextView)convertView.findViewById(R.id.textnumexp);
        holder.nombrecomercial=(TextView)convertView.findViewById(R.id.textnombrecomercial);
        holder.nombrecompleto=(TextView)convertView.findViewById(R.id.textnombrecompleto);
        holder.foto=(ImageView)convertView.findViewById(R.id.searchfoto);
        holder.codficha=(TextView)convertView.findViewById(R.id.textviewcodficha);
        holder.numexp.setText(String.valueOf(listaExpedientes.get(position).getNumexp()));
        holder.nombrecompleto.setText(String.valueOf(listaExpedientes.get(position).getNombrecompleto()));
        holder.nombrecomercial.setText(String.valueOf(listaExpedientes.get(position).getNombrecomercial()));
        holder.foto.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(listaExpedientes.get(position).getFoto()),dpToPx(80), dpToPx(80), true));
        holder.codficha.setText(String.valueOf(listaExpedientes.get(position).getCodficha()));
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    if(holder==null){
        holder=new ViewHolder();
    }



    return convertView;
}

private int dpToPx(int dp)
{
    float density = c.getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}
static class ViewHolder{
    TextView numexp;
    TextView nombrecompleto;
    TextView nombrecomercial;
    ImageView foto;
    TextView codficha;

}
}

The piece of code where the SQL sentence is launched (its inside a thread):

Cursor csr=db.rawQuery(sentencia, null);
listaExpedientes=new ArrayList<BeanListaExpedientes>();
        if(csr.moveToFirst()){

                 do {
                     codficha=csr.getLong(3);
                     codexpediente=csr.getLong(15);
                     nombrecompleto=csr.getString(8);
                     nombrecomercial=csr.getString(13);

                     Cursor csr2=db.rawQuery("select path from fotos where codficha="+codficha+" LIMIT 1", null);
                     if(csr2.moveToFirst()){

                          path=csr2.getString(0);

                                            }
listaExpedientes.add(new BeanListaExpedientes(path, nombrecomercial, nombrecompleto, codexpediente, codficha));
                      }while (csr.moveToNext());
                            refreshAdapter();
                            adapter.notifyDataSetChanged();

Upvotes: 1

Views: 761

Answers (2)

Salah Nour ElDin
Salah Nour ElDin

Reputation: 508

Please remove in the first this line

if(holder==null){
        holder=new ViewHolder();
    }

and use the following line in getview

LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

remove this line from ListaExpedientesAdapter constructor

inflater=LayoutInflater.from(c);

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

those lines

 holder.numexp.setText(String.valueOf(listaExpedientes.get(position).getNumexp()));
 holder.nombrecompleto.setText(String.valueOf(listaExpedientes.get(position).getNombrecompleto()));
 holder.nombrecomercial.setText(String.valueOf(listaExpedientes.get(position).getNombrecomercial()));
 holder.foto.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(listaExpedientes.get(position).getFoto()),dpToPx(80), dpToPx(80), true));
 holder.codficha.setText(String.valueOf(listaExpedientes.get(position).getCodficha()));

go outside the if/else logic. Your getView is called at least getCount times, but only once with convertView = null

Edit,

after you initalize the holder, when convertView is null, don't forget to call convertView.setTag(holder)

Upvotes: 1

Related Questions