带有自定义适配器的 ListView:隐藏了一些项目

ListView with custom adapter: some items are hidden

我有一个带有自定义适配器的 ListView,但是当在 ListView 中滚动时,一些项目被隐藏:

从图中可以看出,从 a69 跳转到 a72,留下隐藏项 a70 和 a71。

这里是自定义适配器的代码:

public NewCurrenciesAdapter(ArrayList<NewCurrencyData> list, Context context) {
    m_list = list;
    m_context = context;
    m_layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
    return m_list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = m_layoutInflater.inflate(R.layout.addcurrency_item, parent, false);
    }

    TextView currencyName = (TextView) convertView.findViewById(R.id.textCurrencyName);
    currencyName.setText("a" + position);

    ImageView currencyImage = (ImageView) convertView.findViewById(R.id.imageCurrencyCountry);

    currencyImage.setImageResource(getImageId(m_context, "c" + m_list.get(position).name.toLowerCase()));

    if (m_list.get(position).checked) {
        convertView.setBackgroundResource(android.R.color.holo_green_dark);
    }
    else {
        convertView.setBackgroundResource(android.R.color.background_light);
    }

    return convertView;
}

编辑:这是 activity 的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/ListViewAllCurrencies"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8">
    </ListView>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Apply"
            android:id="@+id/buttonAdd"
            android:layout_weight=".50"
            android:onClick="applyChange"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Cancel"
            android:id="@+id/buttonCancel"
            android:layout_weight=".50"
            android:onClick="cancelChange"/>
    </LinearLayout>


</LinearLayout>

和addcurrency_item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageCurrencyCountry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cusd"
        android:contentDescription="" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textCurrencyName"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:textSize="20sp"
            android:text="Currency name"/>
    </RelativeLayout>


</LinearLayout>

调试 getImageId(m_context, "c" + m_list.get(position).name.toLowerCase()

返回的整数

它可能返回 0 或 -1,这不会在您的图像视图中设置图像并且视图已折叠!