2011-10-16 104 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被膨胀? –