2012-03-14 28 views
1

我有一個圖像和textview的列表視圖,我在我的類名爲MyArrayAdaptor擴展ArrayAdapter的列表視圖中應用替代行顏色。 當列表視圖首先加載時,會出現替代行顏色,但在滾動列表視圖項目後,顏色變得混合一點,即第二種顏色覆蓋第一種顏色。ListView替代行顏色 - 滾動後獲取不顯示

如何處理這種情況。我很吮吸。

請幫幫我。

在此先感謝

這裏是馬類MyArrayAdaptor:

public class MyArrayAdaptor extends ArrayAdapter<String> { 
    private Activity context; 
    private String[] text1; 
    private String[] image1; 
    private int TextViewID; 
    private int ImageViewID; 
    private int LoyoutID; 
    //private int[] colors = new int[] { 0xFFFFFFFF, 0xb2b2b2FF }; 
    // private int[] colors = new int[] { 0x30FF0000, 0x300000FF }; 
    private int[] colors = new int[] { 0x30FF0000, 0x30000000 }; 


    public MyArrayAdaptor(Activity context,int id, String[] text1,String[] image,int textViewID, int imageViewID) {   
    super(context,id, text1); 
    this.context = context; 
    this.text1 = text1;  
    this.image1=image; 
    this.TextViewID=textViewID; 
    this.ImageViewID=imageViewID; 
    this.LoyoutID=id; 
    } 

    // static to save the reference to the outer class and to avoid access to 
    // any members of the containing class 
    static class ViewHolder { 
    public ImageView imageView; 
    public TextView textView; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // ViewHolder will buffer the assess to the individual fields of the row 
    // layout 

    ViewHolder holder; 

    View rowView = convertView;  

    if (rowView == null) {  
    LayoutInflater inflater = context.getLayoutInflater(); 
    rowView = inflater.inflate(LoyoutID, null, true); 
    //color 
    if (position % 2 == 0) { 
      rowView.setBackgroundColor(colors[0]);   
      } 
     else { 
      rowView.setBackgroundColor(colors[1]);  
     } 
    holder = new ViewHolder(); 
    holder.textView = (TextView) rowView.findViewById(TextViewID); 
    holder.imageView = (ImageView) rowView.findViewById(ImageViewID); 

    rowView.setTag(holder); 

    } else { 
    holder = (ViewHolder) rowView.getTag(); 
    } 
    holder.textView.setText(text1[position]); 

    //holder.imageView.setImageDrawable(LoadImageFromWebOperations(image[position])); 
    holder.imageView.setBackgroundResource(R.drawable.icon); 

    return rowView; 
    } 

    private Drawable LoadImageFromWebOperations(String url) 
    { 
    try  
    { 
    InputStream is = (InputStream) new URL(url).getContent(); 
    Drawable d = Drawable.createFromStream(is, "src name"); 

    return d; 
    }catch (Exception e) { 

    System.out.println("Exc="+e); 

    return null; 

    }} 
    } 

回答

3

我想你應該設置你的背景顏色之外if(rowView == null)條件。例如:剛好在return rowView;

... 

rowView.setBackgroundColor((position%2 == 0)? colors[0] : colors[1]); 

return rowView; 
+0

Thanks.Your code was very helpful。 – user1268300 2012-03-14 07:44:54

+0

謝謝@waqaslam。非常感謝你。這是像魅力.... – 2014-08-05 13:47:55