2017-12-23 17 views
1

すべて動作しています。すなわち、データはFirebaseから読み込まれ、FirebaseListAdapterに表示されます。ここでAndroidリストビュー行の色の変更値(選択時ではない)

は、コードに問題がある

 int urg = model.getUrgency(); 
     if(urg == 1) v.setBackgroundColor(Color.RED); 

問題は、それがランダムにそれが必要以上に赤い色多くの行です。すなわち、私のテストでは、urg = 1のレコードしか作成しませんでしたが、スクロールすると、1行以上が赤に変わります。どうすれば修正できますか?

private void refreshListView(){ 

    fbAdapter = new FirebaseListAdapter<Post>(this, Post.class,R.layout.post_message, query) { 
     @Override 
     protected void populateView(View v, Post model, int position) { 
      TextView messageText = (TextView)v.findViewById(R.id.message_text); 
      TextView messageTime = (TextView)v.findViewById(R.id.message_time); 

      messageText.setText(model.getMessageText()); 
      messageTime.setText(shortTime.format(model.getPostTimeStamp())); 
      int urg = model.getUrgency(); 
      if(urg == 1) v.setBackgroundColor(Color.RED); 
     } 
    }; 

    mListView.setAdapter(fbAdapter); 
    mListView.requestFocus(); 
} 
+0

ここで解決される問題: https://stackoverflow.com/questions/34706585/android-change-background-color-of-specific-item-of-listview –

答えて

1

複数の行で赤色を表示する理由は、elseブロックが存在しないためです。 elseブロックを追加するとこの問題が修正されます。

int urg = model.getUrgency(); 
if(urg == 1){ 
    v.setBackgroundColor(Color.RED); 
}else{ 
    v.setBackgroundColor(Color.WHITE); 
} 
+0

@arshad - ありがとう –

関連する問題