适配器正在返回相同的项目

Adapter is returning the same item

我正在使用 ListView 在其中显示项目,这是单元格的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/lon"
    android:id="@+id/textView5"

    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:id="@+id/foto"
    android:layout_alignParentTop="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/lat"
    android:id="@+id/textView6"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textlon"
    android:layout_alignBottom="@+id/foto"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textlat"
    android:layout_alignTop="@+id/textlon"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

像每个 ListView 一样,它需要一个适配器:

public class FotoAdapter extends BaseAdapter {
private ArrayList<BeanFotos> fotos;
private LayoutInflater inflater=null;
Context c;

public FotoAdapter(Context c, ArrayList<BeanFotos> fotos){
    this.fotos=fotos;
    inflater=LayoutInflater.from(c);
    this.c=c;

}
@Override
public int getCount() {
//this returns the proper size of the array, if I have 3 photos it returns 3
    Log.i("David", "In adapter, the size of the array is: "+fotos.size());
    return fotos.size();
}

@Override
public Object getItem(int position) {
//this is never called
    Log.i("David", "returning item "+position);
    return fotos.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) {
        //This returns 0 always
        Log.i("David", "returning position "+position);
        int bmWidth=fotos.get(position).getFoto().getWidth();
        int bmHeight=fotos.get(position).getFoto().getHeight();
        int ivWidth;
        int ivHeigth;
        int new_width;
        int new_height;
        convertView=inflater.inflate(R.layout.foto_layout, null);
        holder=new ViewHolder();
        holder.lat=(TextView)convertView.findViewById(R.id.textlat);
        holder.lon=(TextView)convertView.findViewById(R.id.textlon);
        holder.foto=(ImageView)convertView.findViewById(R.id.foto);
        ivWidth=dpToPx(80);
        ivHeigth=dpToPx(80);
        new_width=ivWidth;
        new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
        Bitmap newbitMap = Bitmap.createScaledBitmap(fotos.get(position).getFoto(), new_width, new_height, true);
        holder.lat.setText(fotos.get(position).getLatitud().toString());
        holder.lon.setText(fotos.get(position).getLongitud().toString());

        holder.foto.setImageBitmap(newbitMap);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

private int dpToPx(int dp)
{
    float density = c.getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}
static class ViewHolder {
    public TextView lat;
    public TextView lon;
    public ImageView foto;
}
}

如果我设置拍摄照片的调试点,我可以看到我拍摄的不同照片,但在 ListView 中它显示相同的照片(第一张)。如果我在适配器中设置一个调试点,存储包含照片的对象的数组具有正确的大小,并且存储的照片是不同的......但是列表视图总是显示相同的照片,尽管它显示了正确的数量行。

为什么会这样?我该如何解决?有帮助吗?

谢谢。

尝试将以下代码列表放在 if 块的 if else 条件之外,

    int bmWidth=fotos.get(position).getFoto().getWidth();
    int bmHeight=fotos.get(position).getFoto().getHeight();
    ivWidth=dpToPx(80);
    ivHeigth=dpToPx(80);
    new_width=ivWidth;
    new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
    Bitmap newbitMap =Bitmap.createScaledBitmap(fotos.get(position).getFoto(), new_width, new_height,    true);
    holder.lat.setText(fotos.get(position).getLatitud().toString());
    holder.lon.setText(fotos.get(position).getLongitud().toString());

    holder.foto.setImageBitmap(newbitMap);
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
    //This returns 0 always

    convertView=inflater.inflate(R.layout.foto_layout, null);
    holder=new ViewHolder();
    holder.lat=(TextView)convertView.findViewById(R.id.textlat);
    holder.lon=(TextView)convertView.findViewById(R.id.textlon);
    holder.foto=(ImageView)convertView.findViewById(R.id.foto);

    convertView.setTag(holder);
}else{
    holder = (ViewHolder) convertView.getTag();
}
int bmWidth=fotos.get(position).getFoto().getWidth();
int bmHeight=fotos.get(position).getFoto().getHeight();
int ivWidth;
int ivHeigth;
int new_width;
int new_height;
ivWidth=dpToPx(80);
ivHeigth=dpToPx(80);
new_width=ivWidth;
new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
Bitmap newbitMap = Bitmap.createScaledBitmap(fotos.get(position).getFoto(), new_width, new_height, true);
holder.lat.setText(fotos.get(position).getLatitud().toString());
holder.lon.setText(fotos.get(position).getLongitud().toString());
holder.foto.setImageBitmap(newbitMap);

return convertView;
}