开发者

ListView and remembering data

开发者 https://www.devze.com 2023-03-26 00:47 出处:网络
I have some problems with a ListView(it\'s more of a visual problem), it remembers data such as text values from TextViews, from the ListView\'s rows, but tangles or forgets aspects regarding the Text

I have some problems with a ListView(it's more of a visual problem), it remembers data such as text values from TextViews, from the ListView's rows, but tangles or forgets aspects regarding the TextViews background color. The problem shows up only when the ListView contains many rows. I work with a SimpleCursorAdapter and a ViewBinder and want to highlight a TextView if a condition occurs:

In the ViewBinder implementation:

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    CharSequence display;
    if(view.getId() == R.id.dueDate){
        long timestamp = Long.parseLong(cursor.getString(3));
        display=DateUtils.getRelativeTimeSpanString(timestamp); 

        if(timestamp+DaysToMillis(5)> new Date().getTime())
        ((TextView) view).setBackgroundResource(R.color.marker_red);

        ((TextView) view).setText(display);
        return true; 
    }
    else
        return false;
}

So again: when there are many rows the ListView seems to tangle the background color. Some times when I re-scroll, it change's again the TextView's background and I can't seem to find the bug or the ListView's logic.

So, does the setViewValue(..) method gets called every time before inflating a row? Even if you scroll up or down 开发者_StackOverflowand a row that's not visible anymore get's visible again? Or the setViewValue(..) method gets called just for a initial row inflating process and then the created objects are keept in the ListView's memory?

P.S. The text values from the same TextView is displayed correctly. I used the same logic in the same setViewValue(..) method.


Same newbie

It seems to me (and if I'm wrong please correct me... especially if you are Roman Guy) that ViewBinder, with it's setViewValue method recycles the listView's rows...

  • setViewValue(..) gets called for every "FROM - TO" binding for every row displayed, even for rows that were already inflated, and by scrolling up/down they got displayed again;

  • and regarding my amassing piece of code, for a row that gets displayed again: ((TextView) view).setText(display); - will set the correct content, same timestamp will get processed... but if it doesn't passes the:

    if(timestamp+DaysToMillis(5)> new Date().getTime()) ((TextView) view).setBackgroundResource(R.color.marker_red); -the current TextView might have for background the background from a recycled TextView

  • anyways I had to add an else ((TextView) view).setBackgroundResource(0); to my if

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号