2012-10-11 74 views
0

我在嘗試在列表視圖中更改圖像時遇到問題。嘗試更改列表活動中的ImageView的問題

下面是在列表中的行佈局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); 
     } 

任何想法?

+1

對字符串使用'equals()'。 –

+0

就是這樣 - 謝謝Wingman。非常感謝 - 如果你想嘗試一個答案,我會給你打個勾! :) – Scamparelli

+0

Plonked到一個答案:) –

回答

1

的內容比較字符串數據類型時,必須使用equals()equalsIgnoreCase()

對於Java中的字符串:

  • ==比較字符串是否同一目標與否。
  • equals()比較字符串是否具有相同的字符序列與否。
+0

謝謝僚機。你什麼時候會用equalsIgnoreCase()呢? – Scamparelli

+0

爲了確保將「StRIng」與「sTriNG」進行比較並返回true,此方法忽略小寫和大寫的差異。 –

0

嘗試在設置資源後添加flagIcon.invalidate()

+0

謝謝你的快速反應布魯默,但沒有奏效(我試過flagIcon.invalidate();順便說一句,因爲殘廢不被接受)。任何其他想法? – Scamparelli

+0

鑑於僚機的優點,您可以在那裏放置一個Log語句來確認設置ImageResouce的代碼是否確實被達到?另外,如果嘗試使用getResources()。getDrawable(R.drawable.green_flag)而不是setImageResource(),會發生什麼情況? – Blumer

+0

謝謝布盧默,但Wingman是正確的 - 這是邏輯! – Scamparelli

0

,而不是試圖

flagIcon.setImageResource(R.drawable.red_flag); 

你可以試試這個:

flagIcon.setBackgroundResource(R.drawable.red_flag);