Reputation: 11
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
LinearLayout root_view = (LinearLayout) row
.findViewById(R.id.list_root_view);
int evenRow = position % 2;
if (evenRow == 0) {
root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.darker_gray));
}
holder = new ViewHolder();
holder.date = (TextView) row.findViewById(R.id.dateTextView);
holder.time = (TextView) row.findViewById(R.id.timeTextView);
holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
holder.fee = (TextView) row.findViewById(R.id.feeTextView);
holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
historyData = arraylist.get(position);
holder.date.setText(historyData.getDate() + "");
holder.time.setText(historyData.getTime().toString());
holder.breachNo.setText(historyData.getBreachNo() + "");
holder.fee.setText(historyData.getFee() + "");
holder.orderCheckBox.setEnabled(true);
return row;
}
In the above code I have some problem. I want to set my listview
items color alternatively. (for example odd rows in blue color and even rows in grey color). On loading the activity I got the color perfectly. The problem is while scrolling my listview
the items color changing randomly. Can anyone help me to fix..?
Upvotes: 0
Views: 492
Reputation: 2596
You can use
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.root_view = (LinearLayout) row
.findViewById(R.id.list_root_view);
holder.date = (TextView) row.findViewById(R.id.dateTextView);
holder.time = (TextView) row.findViewById(R.id.timeTextView);
holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
holder.fee = (TextView) row.findViewById(R.id.feeTextView);
holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
int evenRow = position % 2;
historyData = arraylist.get(position);
holder.date.setText(historyData.getDate() + "");
holder.time.setText(historyData.getTime().toString());
holder.breachNo.setText(historyData.getBreachNo() + "");
holder.fee.setText(historyData.getFee() + "");
holder.orderCheckBox.setEnabled(true);
if (evenRow == 0) {
holder.root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.darker_gray));
}
else
holder.root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.blue));
return row;
}
and also you should add your LinearLayout
in the ViewHolder
class
class ViewHolder{
LinearLayout root_view;
...
...
...
}
Upvotes: 0
Reputation: 8242
in
getView
set background color, outside setTag if else block . and yes add else as well .
like
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
LinearLayout root_view = (LinearLayout) row
.findViewById(R.id.list_root_view);
int evenRow = position % 2;
if (evenRow == 0) {
root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.darker_gray));
}
holder = new ViewHolder();
holder.date = (TextView) row.findViewById(R.id.dateTextView);
holder.time = (TextView) row.findViewById(R.id.timeTextView);
holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
holder.fee = (TextView) row.findViewById(R.id.feeTextView);
holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
int evenRow = position % 2;
if (evenRow == 0) {
root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.darker_gray));
} else {
root_view.setBackgroundColor(context.getResources().getColor(
android.R.color.blue));
} ......... ............
Upvotes: 2