我创建了一个自定义ArrayAdapter
(下面的代码),它完全正常,直到我滚动。一旦我滚动所有项目变为绿色,我一直在拉我的头发试图找出原因。任何帮助将不胜感激。
自定义适配器旨在使任何ListView项的文本长3个字符 变成绿色而其他所有颜色应保持默认的黑色。
public class FoundAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
static class ViewHolder {
public TextView text;
}
public FoundAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.found, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.found, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.found_txt);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names.get(position);
if(s.length()==3) {
holder.text.setTextColor(0xFF008B45); //green
}
holder.text.setText(s);
return rowView;
}
}
通过以下方式调用.java代码:
adapter=new FoundAdapter(this, array);
ListView view = (ListView) findViewById(R.id.list);
view.setAdapter(adapter);
R.layout.found:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/found_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
/>
</LinearLayout>
答案 0 :(得分:3)
问题是如果条件不满足,则不会重置文本颜色。在Listviews中,视图被回收。
所以做这样的事情
if(s.length()==3) {
holder.text.setTextColor(0xFF008B45); //green
}else{
holder.text.setTextColor(xyz); //xyz
}
答案 1 :(得分:0)
您应该在适配器外部使用此颜色条件将其放在适配器外的数组列表中
if(s.length()==3) {
/**
put your green color in array list
*/
}else{
//put your other color
}
&安培;在您的适配器中只需使用: -
holder.text.setText(list.get(position).colorName);
答案 2 :(得分:0)
public class FoundAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
public FoundAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.found, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.found, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView
.findViewById(R.id.found_txt);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names.get(position);
if (names.get(position).length() == 3) {
holder.text.setTextColor(0xFF008B45); // green
holder.text.setText(s); // green
}
else
{
holder.text.setTextColor(0x11111111); // green
holder.text.setText(s); // green
}
//holder.text.setText(s);
return rowView;
}
}
static class ViewHolder {
public TextView text;
}