2

我创建了一个自定义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>
4

3 回答 3

3

问题是如果条件不满足,您不会重置文本颜色。在 Listviews 中,视图被回收。

所以做这样的事情

if(s.length()==3) {
    holder.text.setTextColor(0xFF008B45); //green
}else{
    holder.text.setTextColor(xyz); //xyz
}
于 2013-07-24T05:13:19.950 回答
0

您应该在适配器外部使用此颜色条件,将其放在适配器外部的数组列表中

       if(s.length()==3) {
       /**
       put your green color in array list 
       */
       }else{
       //put your other color
        }

& 在您的适配器中只需使用:-

 holder.text.setText(list.get(position).colorName);
于 2013-07-24T04:58:58.563 回答
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;
}
于 2013-07-24T05:21:15.233 回答