我有一个从数据库内容填充的列表视图,为此我编写了一个自定义数组适配器。我需要列表视图中的每个项目在一侧都有独特的颜色作为丝带。与以下照片平面设计的部分相同
但我的清单有 20 项因此滚动。当我向下和向上滚动时,颜色的位置会发生变化。当 rowItem.getIds() 等于 0 或 1 或 2 时,它只是第一次进入 switch/case 时发生这种情况。我只是看到第一个 System.out.println 每次都带有“UP”字符串滚动,但其他(其他 system.out.println 没有“UP”)只有一次。为了清除,我附上了我的代码:
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;
}
}
行项目类:
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;
}
}
在主类中,我将 rowItem 如下
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < poemtype.size(); i++) {
RowItem item = new RowItem(poemtype.get(i), i);
rowItems.add(item);
}
我在使用 rowItem.getIds() 之前使用了位置,但结果相同,我尝试了“rowItem.getIds()”,因为我理解位置取决于上下移动获取变化,并且是显示在设备上的项目的位置。