0

当我尝试更改列表视图中的图像时遇到问题。

下面是列表中行的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" 
android:gravity="left">


<ImageView
    android:id="@+id/flagIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.1"
    android:src="@drawable/orange_flag"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/location_row_item_main_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/location_row_item_secondary_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>

运行正常并显示可绘制的状态(即 orange_flag),但是当我尝试在以下代码中更改它时,这并没有改变:

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    public MyListAdapter(Context context, Cursor cursor) { 
        super(context, R.layout.row_location, cursor);
    } 

    @Override
    public void bindView(View view, Context context, Cursor cursor) { 
        TextView title = (TextView) view.findViewById(R.id.location_row_item_main_text);
        title.setText(cursor.getString(
                cursor.getColumnIndex(RMDbAdapter.RACKING_SYSTEM)));
        ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon);
        String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK));
        if (risk == "Red Risk"){
            flagIcon.setImageResource(R.drawable.red_flag);
        }
        else if (risk == "Green Risk"){
            flagIcon.setImageResource(R.drawable.green_flag);
        }
        else if (risk =="No Risk"){
            flagIcon.setImageResource(R.drawable.note);
        }

有任何想法吗?!

4

3 回答 3

1

在比较内容的字符串数据类型时始终使用equals()或。equalsIgnoreCase()

对于java中的字符串:

  • ==比较字符串是否是相同的对象
  • equals()比较字符串是否具有相同的字符序列
于 2012-10-11T17:24:43.500 回答
0

flagIcon.invalidate()设置资源后尝试添加。

于 2012-10-11T16:23:47.530 回答
0

而不是尝试

flagIcon.setImageResource(R.drawable.red_flag);

你可以试试这个:

flagIcon.setBackgroundResource(R.drawable.red_flag);
于 2012-10-11T16:56:00.463 回答