I had a listview that fill from content of database, for that I wrote a custom arrayadapter. I need every item in listview have a unique color as ribbon on one side. same as down part of following photo Flat Design
but my list have 20 items therefore scrolled. when I scrolled down and up the position of color get change. This is happened while it just enter to switch/case just for a first time when rowItem.getIds() is equals to 0 or 1 or 2. I just see the the first System.out.println with the "UP" string every times that scrolled, but the others(Other system.out.println without "UP") just one times.for clearance I attached my codes:
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
String fontType = "";
float fontSize;
int resourceID;
int colour = 0;
int dif = 0;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
resourceID = resourceId;
}
private class ViewHolder {
TextView txtTitle;
View img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
System.out.println("UP " + rowItem.getIds() + " " + rowItem.getTitle());
Typeface type;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.indexitem_row, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView
.findViewById(R.id.textOfRowWithImage);
if (colour == 0)
switch (rowItem.getIds()) {
case 0:
holder.img = convertView.findViewById(R.id.sidebar);
holder.img.setBackgroundColor(Color.rgb(52, 73, 92));
System.out.println(rowItem.getIds() + " "
+ rowItem.getTitle());
break;
case 1:
holder.img = convertView.findViewById(R.id.sidebar);
holder.img.setBackgroundColor(Color.rgb(244, 179, 80));
System.out.println(rowItem.getIds() + " "
+ rowItem.getTitle());
break;
case 2:
holder.img = convertView.findViewById(R.id.sidebar);
holder.img.setBackgroundColor(Color.rgb(92, 151, 191));
System.out.println(rowItem.getIds() + " "
+ rowItem.getTitle());
break;
}
else
holder.img.setBackgroundColor(colour);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setTextSize(fontSize);
holder.txtTitle.setText(rowItem.getTitle());
return convertView;
}
public void setSidebarColor(int i) {
colour = i;
}
}
the RowItemClass:
public class RowItem {
private String title;
private String desc;
int numbers;
public RowItem(String title, int i) {
this.title = title;
this.numbers = i;
}
public RowItem(String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public Integer getIds() {
return numbers;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
if (desc != null)
return title + "\n " + desc;
else
return title;
}
}
and in main class I put rowItem as follow
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < poemtype.size(); i++) {
RowItem item = new RowItem(poemtype.get(i), i);
rowItems.add(item);
}
I used position before used rowItem.getIds() but that was have same result, I tried "rowItem.getIds()" because I understand position depend on the move up and down get change and is the position of item that show on device.