Roshnal
Roshnal

Reputation: 1314

ListView items fading when scrolling in Android

I'm developing for Android and I have an app (called Forget-Me-Not) that uses a ListView as the Tasks screen (Its a ToDo list app). And I have set the items to be grayed out when they are completed.

When the list is bigger than the screen, scrolling occur as usual. But the problem is when scrolled upwards/downwards and come back again, the uncompleted items too have been grayed out. I have checked this many times and the graying-out seems to be random. The items are clickable and they are functioning as expected (i.e. long-clicking, etc). The only problem is the graying-out. (This confuses the users as this app is ToDo list managing app and graying-out is "completing" a task).

I don't know what code to post, so if anyone can tell me what code I should post or could give an answer straightaway, I would be really thankful.

PS: I've got a custom ListAdapter class. It mainly reads the items from SQLite database and sets them to the adapter. You can see the bug in my app- its in the Android Market (Forget-Me-Not).

EDIT: Here is my custom TaskAdapter class:

public class TasksAdapter extends BaseAdapter {

private String[] items;
private int[] priorities;
private Vector<String> completed;
private Context context;

public TasksAdapter(TasksActivity context, int textViewResourceId, String[] items) {
    if(items != null)
        this.items = items;
    else
        this.items = new String[0];

    this.priorities = new int[this.items.length];
    for(int i = 0; i < this.priorities.length; i++) {
        this.priorities[i] = FMN.PRIORITY_LOW;
    }

    this.completed = new Vector<String>(0);

    this.context = context;
}

public TasksAdapter(TasksActivity context, int textViewResourceId, String[] items, int[] priorities, Vector<String> completed) {
    if(items != null)
        this.items = items;
    else
        this.items = new String[0];

    if(priorities != null)
        this.priorities = priorities;
    else {
        this.priorities = new int[this.items.length];
        for(int i = 0; i < this.priorities.length; i++) {
            this.priorities[i] = FMN.PRIORITY_LOW;
        }
    }

    if(completed != null)
        this.completed = completed;
    else
        this.completed = new Vector<String>(0);

    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.task_item, null);
    } 

    TextView task = (TextView) v.findViewById(R.id.tv_task);
    task.setText(items[position]);

    ImageView prio = (ImageView) v.findViewById(R.id.im_task_priority);

    switch(priorities[position]) {
    case FMN.PRIORITY_HIGH:
        prio.setImageResource(R.drawable.fmn_priority_high);
        break;

    case FMN.PRIORITY_MEDIUM:
        prio.setImageResource(R.drawable.fmn_priority_low);
        break;

    case FMN.PRIORITY_LOW:
        prio.setImageResource(R.drawable.fmn_priority_medium);
        break;

    default:
        prio.setImageResource(R.drawable.fmn_priority_medium);
        break;
    }

    if((completed.size() != 0) && (completed.contains(task.getText()))) {
        task.setTextColor(Color.rgb(155, 175, 155));
    }

    return v;
}

public int getCount() {
    return items.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

}

And a screenshot BEFORE scrolling:

Before Scrolling

And AFTER scrolling: After Scrolling

Upvotes: 1

Views: 1208

Answers (1)

Tobias
Tobias

Reputation: 883

It is probably a bug in your ListAdapter getView. Do you recycle your list objects/items? The properties for colors and so on will remain on the object when recycled. Make sure you set the properties correct depending on in which state the task is in.

In the getView function you probably check if the view-item allready is set and then you just change the text for the task on the item (recycling)? At the same place in the code change the color settings for background to the correct task state.

EDIT Now that I see your code it looks like you have got the priorities mixed up, default and priority low set the background to prio.setImageResource(R.drawable.fmn_priority_medium); and medium sets the background to prio.setImageResource(R.drawable.fmn_priority_low);

Ok I found the problem:

if((completed.size() != 0) && (completed.contains(task.getText()))) {
    task.setTextColor(Color.rgb(155, 175, 155));
}

You need an else statement here to set the text color to the original color if it is not completed. Like:

if((completed.size() != 0) && (completed.contains(task.getText()))) {
    task.setTextColor(Color.rgb(155, 175, 155));
} else {
    task.setTextColor(Color.rgb(0, 0, 0));
}

Upvotes: 4

Related Questions