2013-02-01 123 views
0

我已經實現了自定義SimpleCursorAdapter以將圖像和文本插入我的ListView,但是當用戶滾動時圖像四處移動。我怎樣才能讓他們留在原地?這裏是我的代碼(我剝離下來爲簡單起見):滾動ListView混合圖像

final class mySimpleCursorAdapter extends SimpleCursorAdapter { 

public mySimpleCursorAdapter(Context context, int layout, Cursor cur, 
     String[] from, int[] to) { 
    super(context, layout, cur, from, to); 
    mContext = context; 
    mLayoutInflater = LayoutInflater.from(context); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    View v = mLayoutInflater.inflate(R.layout.row, parent, false); 
    return v; 
} 


@Override 
public void bindView(View v, Context context, Cursor c) { 

     ImageView bar11 = (ImageView) v.findViewById(R.id.bar); 
     ImageView bar12 = (ImageView) v.findViewById(R.id.bar2); 
      ImageView bar13 = (ImageView) v.findViewById(R.id.bar3); 
     TextView sign = (TextView) v.findViewById(R.id.text3);       
       sign.setText(currency); 

      if (someValue == 0){ 
       bar11.setVisibility(View.GONE); 
      } 

      if (someValue == 1){ 
       bar12.setVisibility(View.VISIBLE); 
      }   

      if (someValue == 2){ 
       bar13.setVisibility(View.VISIBLE); 
      } 

    } 
} 

row.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/bar" 

    android:layout_width="fill_parent" 

    android:layout_height="65dp" 

    android:background="@drawable/greenbar"/> 

    <ImageView 
    android:id="@+id/bar2" 

    android:layout_width="fill_parent" 

    android:layout_height="65dp" 

    android:background="@drawable/two"/> 

    <ImageView 
    android:id="@+id/bar3" 

    android:layout_width="fill_parent" 

    android:layout_height="65dp" 

    android:background="@drawable/two"/> 


<TextView 

    android:id="@+id/text3" 

    android:layout_width="wrap_content" 

    android:layout_height="fill_parent" 

    android:layout_alignParentRight="true" 

    android:layout_centerVertical="true"/> 

</RelativeLayout> 

List.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/expense" 
android:background="@drawable/expensebackgroundblue"> 


<ListView 
    android:id="@+id/contentlist" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#00000000" 
    android:cacheColorHint="#00000000" > 
</ListView> 

</RelativeLayout> 
+0

你的檢查應該看起來像這樣if(someValue == 0){ bar11.setVisibility(View.GONE); }其他{bar11.setVisibility(View.VISIBLE);}'由於查看回收 – Amal

+0

嗯,我實際上有3個圖像,這意味着我需要至少有兩個「如果」,這就是我遇到問題的地方.. 。我應該早點指定 – 725623452362

+0

你可以在這裏粘貼你的xml嗎? –

回答

1

問題發生的原因查看回收你需要做什麼來將視圖可見性設置爲可見或不存在如下

@Override 
public void bindView(View v, Context context, Cursor c) { 

    ImageView bar11 = (ImageView) v.findViewById(R.id.bar); 
    ImageView bar12 = (ImageView) v.findViewById(R.id.bar2); 
     ImageView bar13 = (ImageView) v.findViewById(R.id.bar3); 
    TextView sign = (TextView) v.findViewById(R.id.text3);       
      sign.setText(currency); 

     if (someValue == 0){ 
      bar11.setVisibility(View.GONE); 
     }else 
     { 
      bar11.setVisibility(View.VISIBLE); 
     } 

     if (someValue == 1){ 
      bar12.setVisibility(View.VISIBLE); 
     }else 
     { 
      bar12.setVisibility(View.GONE); 
     }   

     if (someValue == 2){ 
      bar13.setVisibility(View.VISIBLE); 
     }else 
     { 
      bar13.setVisibility(View.GONE); 
     } 
} 
+0

謝謝你這個作品! – 725623452362