适配器返回同一项目两次

时间:2015-07-30 10:29:14

标签: android android-listview android-adapter

我有一个ListView包含一些项目。它显示了数据库中搜索的结果,其中两个项符合选择句的标准。

当我第一次进行搜索时,它会返回相同项目的两倍。但是,当我再次点击搜索按钮时,这次它就完成了,显示了两个项目。我该怎么办才能第一次正确地获取物品?

适配器:

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;

}
}

启动SQL语句的代码段(在线程内部):

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();

2 个答案:

答案 0 :(得分:1)

那些行

 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()));

走出if / else逻辑。您的getView被称为至少getCount次,但只有convertView = null

编辑,

在您holder初始化后,convertView为空时,请勿忘记致电convertView.setTag(holder)

答案 1 :(得分:1)

请在第一行删除

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

并在getview中使用以下行

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

ListaExpedientesAdapter构造函数

中删除此行
inflater=LayoutInflater.from(c);