我在使用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>
我可以看到你的意思。但是,通過這段代碼,ListView中的每一行都會獲得黑色。 – Bastaix
你在背景色中設置了else子句中的位置!= {6,7}的情況?如果將其設置爲紅色,則應該看到除6和7之外的所有背景均爲紅色。 –
當位置!= {6,7}時,我設置默認顏色。但隨着你的更正,每一行都變黑了? – Bastaix