Я проработал различные ответы о том, как чередовать цвет фона в виде списка, но ни один из способов не работает должным образом.android listview чередующийся цвет фона
Я продлил SimpleCursorAdapter
и установить цвет фона в зависимости от номера строки:
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
private ViewHolder holder;
private int layoutRessource;
private LayoutInflater mInflater;
public MySimpleCursorAdapter(Context applicationContext, int layout, Cursor cursor,
String[] from, int[] to) {
super(applicationContext, layout, cursor, from, to, 0);
layoutRessource = layout;
mInflater = LayoutInflater.from(applicationContext);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
holder.getTextAbove().setText(cursor.getString(cursor.getColumnIndex("_id")));
holder.getTextBelow().setText(cursor.getString(cursor.getColumnIndex("ICDTxt")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(TAG, "entered newView");
View view = mInflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.setTextAbove((TextView) view.findViewById(R.id.text_code));
holder.setTextBelow((TextView) view.findViewById(R.id.text_description));
holder.setLayout((LinearLayout) view.findViewById(R.id.rowLayout));
view.setTag(holder);
return view;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "entered get View");
Log.d(TAG, "convertView == null: " + (convertView == null));
View rowView = super.getView(position, convertView, parent);
if (position % 2 == 0) {
rowView.setBackgroundColor(Color.BLUE);
// rowView.setBackgroundResource(R.color.listBackgroundEven);
} else {
rowView.setBackgroundResource(R.color.listBackgroudOdd);
}
return rowView;
}
private class ViewHolder {
private TextView textAbove = null, textBelow = null;
public TextView getTextAbove() {
return textAbove;
}
public void setTextAbove(TextView textAbove) {
this.textAbove = textAbove;
}
public TextView getTextBelow() {
return textBelow;
}
public void setTextBelow(TextView textBelow) {
this.textBelow = textBelow;
}
}
Обычай LayoutXML для линий ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_code"
android:textAppearance="@android:style/TextAppearance.Holo.Large"
android:textColor="@color/fontColor"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_description"
android:textAppearance="@android:style/TextAppearance.Holo.Medium"
android:textColor="@color/fontColor"/>
</LinearLayout>
Это работает, но только изменяет backgroundcolor текста в каждой строке. Я хотел бы изменить цвет фона всей строки. Что-нибудь мне не хватает?
Изменить цвет фона 'listView' на свой' xml'. Похоже, что вы chaning цвет 'ListView item' – hrskrs
Конечно, предмет должен быть сделан. Установите цвет convertView напрямую. Удалите этот rowView полностью. – greenapps
Если я попытаюсь setBackground непосредственно на convertView, я получаю исключение с нулевым указателем. – user3609367