2016-10-05 14 views
0

ListViewに1つのアダプタを作成しました。指定した値があれば、プログラムによって行の色を変更したいと思います。スクロール時にListViewのフォーマットが失われる

以下のコードは、この実装方法を示しています。 2つの奇妙なことが起こります:

1)最初の行はペイントされている必要があります。

2)その他の行(最初を除く)はカラフルですが、スクロールを実行すると背景色が失われます。

public View getView (int position, View convertView, ViewGroup parent) { 
     convertView = (RelativeLayout) inflater.inflate(resource, null); 
     Content Legend = getItem(position); 

     TextView name = (TextView) convertView.findViewById(R.id.name); 
     name.setText(Legend.getName()); 

     TextView value = (TextView) convertView.findViewById(R.id.value); 

     if (Legend.getValue().equals("Color")){ 
      convertView.setBackgroundColor(Color.GRAY); 
      Legend.setValue(""); 
     }*/ 
     value.setText(Legend.getValue()); 

     return convertView; 
    } 

答えて

0

listViewのビューは再利用されるので、各ビューを1か所に設定すると思うようにしてください。だから、他の{}

if (Legend.getValue().equals("Color")){ 
     convertView.setBackgroundColor(Color.GRAY); 
     Legend.setValue(""); 
    } else { 
    //set background and other value if not equals "color" 

}

0
static class ViewHolderItem { 
    TextView name, value; 
public ViewHolderItem(View view) { 
      name = (TextView) view.findViewById(R.id.name); 
value = (TextView) view.findViewById(R.id.value); 
} 


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

     ViewHolderItem holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.xxx, parent, false); 
      holder = new MyHolder(convertView); 
      convertView.setTag(holder); 
     } else { 
      holder = (MyHolder) convertView.getTag(); 
     } 
      Content Legend = getItem(position); 
      holder.name.setText(Legend.getName()); 

if (Legend.getValue().equals("Color")){ 
      convertView.setBackgroundColor(Color.GRAY); 
      holder.value.setText("whatever you wanna show or keep it empty"); 
      //whatever you wanna do 
     } 
else{ 
holder.value.setText(Legend.getValue()); 
} 
を追加し、すべての状況を考慮して必要とします
関連する問題