2012-05-02 8 views
1

行の背景色を、その子のテキスト値に基づいて設定しています。ただし、テキストの値に関係なく、複数の背景が設定されています。上下にスクロールすると悪化します。アダプタのコード:あなたができる読み込みとスクロール時に混在した色が出るリストビューの行

package com.test.app; 

import java.util.ArrayList; 

import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MyBaseAdapter extends BaseAdapter { 

    private LayoutInflater mInflater = null; 
    private ArrayList<String[]> mItems = new ArrayList<String[]>(); 

    public MyBaseAdapter(Context context, ArrayList<String[]> items) { 
     mItems = items; 
     mInflater = LayoutInflater.from(context); 
    } 

    public void addItem(String[] it) { 
     mItems.add(it); 
    } 

    public void setListItems(ArrayList<String[]> lit) { 
     mItems = lit; 
    } 

    @Override 
    public int getCount() { 
     return mItems.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mItems.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    static class ViewHolder { 
     public TextView tv0,tv1; 
    } 

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

     View rowView = null; 
     ViewHolder viewHolder; 

     if(convertView == null) 
     { 
      rowView = mInflater.inflate(R.layout.history_row, null); 
     } 
     else 
     { 
      rowView = convertView; 
     } 

     viewHolder = new ViewHolder(); 

     viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0); 
     viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1); 

     rowView.setTag(viewHolder); 

     ViewHolder holder = (ViewHolder) rowView.getTag(); 

     holder.tv0.setText(mItems.get(position)[0].toString()); 
     holder.tv1.setText(mItems.get(position)[1].toString()); 

     if(holder.tv1.getText().equals("0")) 
     { 
      rowView.setBackgroundColor(0xAA777777); 
      // Only the row containing "0" should be colored, yet it colors multiple, random rows. 
     } 

     return rowView; 
    } 

} 

答えて

4

あなただけを使用する場合:

if(holder.tv1.getText().equals("0")) { 
      rowView.setBackgroundColor(0xAA777777); 
      // Only the row containing "0" should be colored, yet it colors multiple, random rows. 
} 

これは確かに0にを含む行を行いますその特定の色を持っていますが、リストを上下にスクロールさせると、この色を持つこの特定の行はリサイクルされ、終わってはいけない場所で終わります。

if(holder.tv1.getText().equals("0")) { 
      rowView.setBackgroundColor(0xAA777777); 
      // Only the row containing "0" should be colored, yet it colors multiple, random rows. 
} else { 
    rowView.setBackgroundColor(/*Here the default color will be*/) 
    // This row doesn't contain 0 so it must have the default color. 
    // Because you could be dealing with a recycled view(that has the above color) 
// then we must revert the color to the default to be sure we end up with the correct color. 
} 

はまたViewHolderパターンの正しいコードは次のとおりです:

正しい方法は、あなたが可能なリサイクルビューの悪い色を上書きするので、それは 0が含まれていない場合は、行にデフォルトの色を提供することです
View rowView = convertView; 
    ViewHolder viewHolder; 

    if(rowView == null) { 
     rowView = mInflater.inflate(R.layout.history_row, parent, false); 
     viewHolder = new ViewHolder(); 
     viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0); 
     viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1); 
     rowView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) rowView.getTag();  
    }    

を入力し、viewHolderオブジェクトを使用して行のデータをセットアップします。

+0

よくやった、素晴らしい説明は、私が求めたものだけです。私は15の担当者を打つとすぐに戻ってupvoteします。 – gatzkerob

+0

唯一の問題は、 'rowView'の背景が設定されると、上下にスクロールして元の色にリセットすることです。 **編集:私はちょうどその問題を解決しました:** 次の関数を使ってtv0のテキストを手動で "0"に設定すると、トリックが実行されます.. 'public void addSingleItem(int aLPos、int strPos、String it){ mItems.get(aLPos)[strPos] =それ; } ' – gatzkerob

+1

完了!あなたは私の日Luksprogを作った!どうもありがとうございます ! – BSQ

0

リストビューとして設定し、それをしようとする:android:cacheColorHint="#00000000"

+0

これは機能しませんでした。いくつかの検索の後、同様の問題がいくつか見つかりましたが、自分自身のコードでそれらを実装できないようです。 [例1](http://stackoverflow.com/questions/3121153/baseadapter-causing-listview-スクロール時の不在時) [例2](http://stackoverflow.com/questions/2955218/listview-in-arrayadapter-order-gets-mixed-up-when-scrolling ) – gatzkerob

関連する問題