适配器两次返回相同的项目
Adapter returning same item twice
我有一个包含一些项目的 ListView。它显示了在数据库中搜索的结果,其中两个项目符合 select 句子的条件。
当我第一次搜索时,它 returns 两次搜索相同的项目。但是,当我再次单击搜索按钮时,这次一切正常,显示了两个项目。我该怎样做才能第一时间正确拿到物品?
适配器:
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();
那些行
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
为null时,不要忘记调用convertView.setTag(holder)
请删除第一行
if(holder==null){
holder=new ViewHolder();
}
并在 getview 中使用以下行
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
从 ListaExpedientesAdapter
构造函数中删除这一行
inflater=LayoutInflater.from(c);
我有一个包含一些项目的 ListView。它显示了在数据库中搜索的结果,其中两个项目符合 select 句子的条件。
当我第一次搜索时,它 returns 两次搜索相同的项目。但是,当我再次单击搜索按钮时,这次一切正常,显示了两个项目。我该怎样做才能第一时间正确拿到物品?
适配器:
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();
那些行
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
为null时,不要忘记调用convertView.setTag(holder)
请删除第一行
if(holder==null){
holder=new ViewHolder();
}
并在 getview 中使用以下行
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
从 ListaExpedientesAdapter
构造函数中删除这一行
inflater=LayoutInflater.from(c);