Scamparelli
Scamparelli

Reputation: 744

Issue trying to change ImageView in List Activity

I am having problems when I try to alter the image in a list view.

Below is the layout XML for the row in the list:

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

Runs ok and shows the drawable stated (i.e. orange_flag), but this doesn't change when I try and alter it in the following code:

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);
        }

Any ideas?!

Upvotes: 0

Views: 135

Answers (3)

s.d
s.d

Reputation: 29436

Always use equals() or equalsIgnoreCase() when comparing String data type for the contents.

For strings in java:

  • == compares whether strings are same Object or not.
  • equals() compares whether strings have same sequence of characters or not.

Upvotes: 1

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10650

Instead of trying

flagIcon.setImageResource(R.drawable.red_flag);

You can try this :

flagIcon.setBackgroundResource(R.drawable.red_flag);

Upvotes: 0

Blumer
Blumer

Reputation: 5035

Try adding flagIcon.invalidate() after setting the resource.

Upvotes: 0

Related Questions