android CustomListView - 图像视图覆盖

android CustomListView - imageview override

我有一个带有自定义行布局和自定义适配器的 ListView。 除了图像,一切都很好。 我想给任何行另一个图像,但结果是所有行都具有相同的图像。 代码如下:

Row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:longClickable="true">

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

        <ImageView
            android:id="@+id/menu_item_icon"
            android:layout_width="91dp"
            android:layout_height="91dp"

            android:longClickable="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
        </ImageView>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:id="@+id/menu_item_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/label"
            android:textSize="40px"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/menu_item_icon"
            android:layout_toEndOf="@+id/menu_item_icon"
            android:textStyle="bold"
            android:longClickable="true">
        </TextView>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Price: "
                android:textStyle="bold"
                android:id="@+id/menu_item_price_lable"
                android:longClickable="true"
                android:layout_below="@+id/menu_item_label"
                android:layout_toRightOf="@+id/menu_item_icon"
                android:layout_toEndOf="@+id/menu_item_icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/menu_item_price_value"
                android:longClickable="true"
                android:layout_below="@+id/menu_item_label"
                android:layout_toRightOf="@+id/menu_item_price_lable"
                android:layout_toEndOf="@+id/menu_item_price_lable" />
        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Time To Make: "
                android:id="@+id/menu_item_time_lable"
                android:longClickable="true"
                android:textStyle="bold"

                android:layout_below="@+id/menu_item_price_lable"
                android:layout_toRightOf="@+id/menu_item_icon"
                android:layout_toEndOf="@+id/menu_item_icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/menu_item_time_value"
                android:longClickable="true"
                android:layout_alignTop="@+id/menu_item_time_lable"
                android:layout_toRightOf="@+id/menu_item_time_lable"
                android:layout_toEndOf="@+id/menu_item_time_lable" />
        </LinearLayout>

    </LinearLayout>
    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="90dp"
        android:id="@+id/menuItemNumberPicker"
        android:clickable="true"
        android:layoutMode="clipBounds"
        android:nestedScrollingEnabled="true"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/menu_item_time_value"
        android:layout_toEndOf="@+id/menu_item_time_value" />

</LinearLayout> 

适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.rowlayout, parent, false);
    }
    TextView lable = (TextView) convertView.findViewById(R.id.menu_item_label);
    TextView price = (TextView) convertView.findViewById(R.id.menu_item_price_value);
    TextView time = (TextView) convertView.findViewById(R.id.menu_item_time_value);


    MazeMenuItem mazeMenuItem;

    mazeMenuItem = allItems.get(position);


    if (mazeMenuItem != null) {
        lable.setText(mazeMenuItem.getTitle());
        price.setText(String.valueOf(mazeMenuItem.getPrice()));
        time.setText(String.valueOf(mazeMenuItem.getTimeToMake()));
    }

    NumberPicker number = (NumberPicker) convertView.findViewById(R.id.menuItemNumberPicker);
    number.setMinValue(0);
    number.setMaxValue(10);//Just for test

    //TODO add image
    ImageView imageView = (ImageView) convertView.findViewById(R.id.menu_item_icon);
    switch (mazeMenuItem.getTitle()){
        case "Macchiato":
            imageView.setImageResource(R.drawable.macchiato);
        case "Esspresso":
            imageView.setImageResource(R.drawable.esspresso);
        case "BlackCoffee":
            imageView.setImageResource(R.drawable.blackcoffee);
        case "Americano":
            imageView.setImageResource(R.drawable.americano);
        case "Late":
            imageView.setImageResource(R.drawable.lathe);
    }

    return convertView;
}

谢谢!

您忘记在 case 语句中使用 "break;"。所以你的应用程序总是会通过案例掉下来并显示 "Late"。 这是正确的开关块:

switch (mazeMenuItem.getTitle()){
    case "Macchiato":
        imageView.setImageResource(R.drawable.macchiato);
        break;
    case "Esspresso":
        imageView.setImageResource(R.drawable.esspresso);
        break;
    case "BlackCoffee":
        imageView.setImageResource(R.drawable.blackcoffee);
        break; 
    case "Americano":
        imageView.setImageResource(R.drawable.americano);
        break;
    case "Late":
        imageView.setImageResource(R.drawable.lathe);
        break;

    default:
        Log.wtf(APP_TAG, "Unsupported coffee title: " + mazeMenuItem.getTitle());
}