2012-11-05 179 views
2

Befor使用視圖我能夠改變我的列表視圖的一個元素(用te position == numEmission)的背景,但現在每個tiem我滾動彩色項目變化和我甚至可以有3種顏色的物品,任何一個都可以幫助我。更改列表視圖的一個元素的背景顏色

public class ItemAdapterProgramme extends ArrayAdapter<Programme> { 

public ArrayList<Programme> resultats; 
public Bitmap bm; 
public Context context; 
public ImageLoader imageLoader = ImageLoader.getInstance(); 
public DisplayImageOptions options; 
static ViewHolder holder; 
public LayoutInflater mInflater ; 
public int numEmission; 
public Calendar c; 
public int cHour; 
Typeface typeface_date, typeface_title; 

public ItemAdapterProgramme(Context context, ArrayList<Programme> resultat) { 
    super(context, R.layout.feed_view_programme, resultat); 
    this.context = context; 
    resultats = resultat; 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    typeface_date = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Medium.ttf"); 
    typeface_title = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Condensed.ttf"); 
} 

public View getView(final int position, View convertView, final ViewGroup parent) { 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.feed_view_programme,parent, false); 
     holder = new ViewHolder(); 
     holder.image = (ImageView) convertView.findViewById(R.id.prog_logo); 
     holder.titre = (TextView) convertView.findViewById(R.id.prog_title); 
     holder.animator = (TextView) convertView.findViewById(R.id.prog_anim); 
     holder.start = (TextView) convertView.findViewById(R.id.prog_start); 
     holder.end = (TextView) convertView.findViewById(R.id.prog_end); 
     holder.description = (TextView) convertView.findViewById(R.id.prog_description); 
     holder.animator.setTypeface(typeface_date); 
     holder.titre.setTypeface(typeface_title); 
     holder.start.setTypeface(typeface_title); 
     holder.end.setTypeface(typeface_title); 
     holder.description.setTypeface(typeface_title); 
     holder.pb = (ProgressBar) convertView.findViewById(R.id.prog_progressBar1); 
     holder.relativeLout_emission = (RelativeLayout)convertView.findViewById(R.id.layout_prog); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.titre.setText(resultats.get(position).getTitle()); 
    holder.animator.setText(resultats.get(position).getAnimateur()); 
    holder.start.setText("Début: "+resultats.get(position).getStart()); 
    holder.end.setText(" Fin: "+resultats.get(position).getEnd()); 
    holder.description.setText(resultats.get(position).getDescription()); 




    File cacheDir = StorageUtils.getOwnCacheDirectory(context, "UniversalImageLoader/Cache"); 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) 
    .denyCacheImageMultipleSizesInMemory() 
    .offOutOfMemoryHandling() 
    .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) 
    .discCache(new UnlimitedDiscCache(cacheDir)) 
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
    .imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) 
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) 
    .enableLogging() 
    .build(); 

    imageLoader.init(config); 

    options = new DisplayImageOptions.Builder() 
    .showStubImage(R.drawable.error) 
    .showImageForEmptyUri(R.drawable.error) 
    .cacheInMemory() 
    .cacheOnDisc() 
    .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
    .build(); 

    imageLoader.displayImage(resultats.get(position).getImageUrl(), holder.image, options, new ImageLoadingListener() { 
     @Override 
     public void onLoadingStarted() { 
      holder.pb.setVisibility(View.VISIBLE);     } 
     @Override 
     public void onLoadingFailed(FailReason failReason) { 
      holder.pb.setVisibility(View.GONE);     
     } 
     @Override 
     public void onLoadingComplete(Bitmap loadedImage) { 
      holder.pb.setVisibility(View.GONE);     
     } 
     @Override 
     public void onLoadingCancelled() { 
      // Do nothing 
     } 
    }); 
    c = Calendar.getInstance(); 
    cHour=c.get(Calendar.HOUR_OF_DAY); 
    Log.d("Adapter cHour", cHour+""); 
    numEmission=findEmission(cHour); 
    Log.d("Adapter numEmission", numEmission+""); 
    if(position==numEmission){ 
     holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.grey)); 
    } 
    return convertView; 
} 

public int findEmission(int hour){ 
    for(int i=0;i < resultats.size()-1;i++){ 
     String start=resultats.get(i).getStart(); 
     String end=resultats.get(i).getEnd(); 
     String[] P0=start.split(":"); 
     String[] P1=end.split(":"); 
     start=P0[0]; 
     end=P1[0]; 
     int intStart=Integer.parseInt(start); 
     int intEnd=Integer.parseInt(end); 
     if(intStart<=hour && hour<=intEnd) { 
      numEmission=i; 
      break; 
     } 
    } 
    return numEmission; 
} 

static class ViewHolder { 
    TextView titre, animator, start, end, description; 
    ImageView image; 
    ProgressBar pb; 
    RelativeLayout relativeLout_emission; 
} 

回答

7

問題不在於持有者,而是在convertView。 ListView可以多次使用一個convertView來提高性能。所以如果你給一個物品着色一次,每次它被重複使用,你將有一個更多的彩色物品。您可以每次移除固定器並充氣新視圖,但這樣做不好。

嘗試modifiying這樣

if (position==numEmission) { 
    holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.grey)); 
} else { 
    // set default background color 
    holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.default_list_element_background)); 
} 
+0

你的代碼非常感謝你:) –

+0

+1了確切的答案。 – jeet

+0

我不能接受答案,我必須再等3分鐘:) –