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

时间:2015-02-28 08:18:15

标签: android android-layout android-listview android-adapter

我有一个带有自定义适配器的ListView,但在ListView中滚动时,某些项目被隐藏:enter image description here

从图像中你可以看到从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;
}

编辑:以下是活动的布局:

<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>

1 个答案:

答案 0 :(得分:0)

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

返回的int

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