我有一个自定义ListView
,我需要显示绿色Color
的可用班次和红色的不可用班次。我在其中创建了一个自定义适配器getView()
我正在尝试检查值并更改颜色。
自定义适配器中的GetView看起来像这样 -
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v==null){
v = inflater.inflate(R.layout.simplerow, null);
}
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
但是我的结果是这样 -
每当我滚动颜色时,也会显示其他值。
答案 0 :(得分:2)
将您getView()
替换为
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/**
* here check your condtion like this and set color to background no need of for loop
***/
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}else
{
// set normal color here
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
答案 1 :(得分:1)
在为一个视图设置红色背景时,您必须使用其他情况
为剩余视图设置正常背景if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else{
//here set normal background for view.
}
希望这会对你有所帮助。
答案 2 :(得分:1)
这是由于ListView的View Reuse功能。如果不满足Shift -3
条件,则必须将行的背景设置为默认行颜色,因为具有红色背景的行可以在列表中的任何位置重复使用。
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(R.drawable.red_bg);
} else {
//default bg
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(deafult_bg);
}
答案 3 :(得分:1)
这是解决方案。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else {
//Update to default color
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
如果条件匹配,则将RED
颜色设置为默认颜色。
答案 4 :(得分:1)
您还需要在此处添加其他部分,如下所示 -
for (int i = 1; i < 9 ; i++) {
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
else{
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB ackgroundResource(R.drawable.green_bg); //your green color
}
}
答案 5 :(得分:0)
您必须使用getViewType()
。试试这段代码(我为你修改了代码):
@Override
public int getItemViewType(int position) {
return (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) ? 1 : 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(type == 0) {
//Set here your regular color
}else {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
编辑:我必须解释一下。如果您不使用getViewType()
方法,并且只使用其他答案中的语句,那么如果您的列表中有很多项目(足以滚动),您的项目将在滚动时动态更改颜色。