2011-10-16 165 views
0

我在使用ListView中的不同顏色時遇到了一些問題。不同顏色的Android ListView

我已經試過幾件事情,但似乎沒有任何工作

私有類GameRowAdapter擴展ArrayAdapter { ArrayList的對象;

public GameRowAdapter(Context context, int textViewResourceId, ArrayList<RowModel> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     RowModelViews model; 
     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this); 
      row = inflater.inflate(R.layout.game_line_layout, null); 
      model = new RowModelViews(); 
      model.entry = (TextView) row.findViewById(R.id.gameLineEntry); 
      model.score = (TextView) row.findViewById(R.id.gameLineEntryScore); 
      row.setTag(model); 
     } else { 
      model = (RowModelViews) row.getTag(); 
     } 

     if (position == 6 || position == 7) { 
      row.setBackgroundColor(R.color.black); 
     } else { 
      row.setBackgroundColor(R.color.light_transparent); 
     } 

     model.entry.setText(objects.get(position).entry); 
     model.score.setText(objects.get(position).score); 

     return row; 
    } 
} 

static class RowModelViews { 
    TextView entry; 
    TextView score; 

} 

static class RowModel { 
    String entry; 
    String score; 

    public RowModel(String entry, String score) { 
     this.entry = entry; 
     this.score = score; 
    } 
} 

任何人都可以告訴我,我做錯了什麼?

謝謝。

UPDATE

這裏是inflatet行

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal"> 
    <TextView android:id="@+id/gameLineEntry" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:layout_marginLeft="20dp" 
       android:textSize="20dp" 
       android:gravity="center_vertical"/> 
    <TextView android:id="@+id/gameLineEntryScore" 
       android:layout_height="fill_parent" 
       android:layout_width="65dp" 
       android:layout_marginRight="20dp" 
       android:gravity="center" 
       style="@style/Button"     
       android:layout_gravity="right"/> 
</LinearLayout> 

回答

0

編輯:在運行一個小的本地測試後,我認爲問題可能是您在每行中的TextViews在ListView行視圖的前面。嘗試設置model.entry和model.score背景顏色。 /編輯

我認爲它來自convertViews被重用。嘗試移動這樣的:

if (position == 6 || position == 7) { 
    row.setBackgroundColor(R.color.black); 
} 

的如果外(行== NULL)塊,並將其改成這樣:

if (position == 6 || position == 7) { 
    model.entry.setBackgroundColor(R.color.black); 
    model.score.setBackgroundColor(R.color.black); 

} else { 
    model.entry.setBackgroundColor(/*Whatever your default background color is*/); 
    model.score.setBackgroundColor(/*Whatever your default background color is*/); 
} 

所以你完全getView方法是:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     RowModelViews model; 
     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this); 
      row = inflater.inflate(R.layout.game_line_layout, null); 
      model = new RowModelViews(row); 
      model.entry = (TextView) row.findViewById(R.id.gameLineEntry); 
      model.score = (TextView) row.findViewById(R.id.gameLineEntryScore); 
      row.setTag(model); 
     } else { 
      model = (RowModelViews) row.getTag(); 
     } 

     if (position == 6 || position == 7) { 
      model.entry.setBackgroundColor(R.color.black); 
      model.score.setBackgroundColor(R.color.black); 
     } else { 
      model.entry.setBackgroundColor(/*Whatever your default background color is*/); 
      model.score.setBackgroundColor(/*Whatever your default background color is*/); 
     } 

     model.entry.setText(objects.get(position).entry); 
     model.score.setText(objects.get(position).score); 

     return row; 
    } 
+0

我可以看到你的意思。但是,通過這段代碼,ListView中的每一行都會獲得黑色。 – Bastaix

+0

你在背景色中設置了else子句中的位置!= {6,7}的情況?如果將其設置爲紅色,則應該看到除6和7之外的所有背景均爲紅色。 –

+0

當位置!= {6,7}時,我設置默認顏色。但隨着你的更正,每一行都變黑了? – Bastaix

2

你只定義在創建視圖的顏色的XML。但是對於再循環視圖,您需要再次明確設置顏色。不能保證6和7是新的觀點。所以總是設置視圖的顏色與每次調用getView

+0

我試圖爲每個getView調用設置顏色(請參閱Bernstein答案),但是當我這樣做時,每行都變成黑色? – Bastaix

+0

你能發佈你嘗試過的新方法嗎?你也可以顯示xml被膨脹? –