Matt Stokes
Matt Stokes

Reputation: 4958

Android custom ArrayAdapter not staying on scroll

I have created a custom ArrayAdapter (code below) which works perfectly fine until I scroll. Once I scroll all items go green and I have been pulling my hair trying to figure out why. Any help would be greatly appreciated.

The custom adapter is intended to make the text of any ListView item 3 characters long turn green while all others should remain the default color of black.

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;
  }
} 

Called in the .java code via:

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>

Upvotes: 2

Views: 654

Answers (3)

QuokMoon
QuokMoon

Reputation: 4425

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;
}

Upvotes: 0

Abhishek Nandi
Abhishek Nandi

Reputation: 4275

The problem is that you are not resetting the text color if the condition does not satisfy. In Listviews views are recycled.

so do something like this

if(s.length()==3) {
    holder.text.setTextColor(0xFF008B45); //green
}else{
    holder.text.setTextColor(xyz); //xyz
}

Upvotes: 3

Deepanker Chaudhary
Deepanker Chaudhary

Reputation: 1704

You should use this color condition outside the adapter put it on your array list outside your adapter

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

& in your adapter simply use :-

 holder.text.setText(list.get(position).colorName);

Upvotes: 0

Related Questions