开发者

android: getView() problem - it changes items while scrolling

I made a listview that has three textviews and a checkbox and put it into a BaseAdapter. What it has to do is if an item is at the 'unread' status, then make its textviews BOLD. However, I encountered two problems.

  1. the top item is always BOLD no matter it is read or unread.
  2. If I do scrolling, then items reloaded by scrolling start to be bold

I researched about it but couldn't find any good things for me. If anyone has an idea, would you please help me? Below is getView().


public View getView(final int position, View convertView, ViewGroup parent) {
            View view = c开发者_开发知识库onvertView;
            ViewHolder viewHolder;

            if(convertView == null){ // check if convertView exists
                // get and inflate layout
                view = layoutInflater.inflate(R.layout.row, null);

                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView)view.findViewById(R.id.list_callerIDname);
                viewHolder.tv2 = (TextView)view.findViewById(R.id.list_callerIDnumber);
                viewHolder.tv3 = (TextView)view.findViewById(R.id.list_messageSentTime);
                viewHolder.cb = (CheckBox)view.findViewById(R.id.checkBox1);

                // get each CheckBox into cb_array for future use
                this.cb_array[position] = (CheckBox)view.findViewById(R.id.checkBox1);

                // use it as a tag
                view.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder)view.getTag();
            }

            viewHolder.tv1.setText(this.callerIDnames[position]);
            viewHolder.tv2.setText(this.callerIDnumbers[position]);
            viewHolder.tv3.setText(this.messageSentTimes[position]);

            // if message is unread, then make texts bold
            if (messageRead_list[position] == false){
                viewHolder.tv1.setTypeface(viewHolder.tv1.getTypeface(), Typeface.BOLD);
                viewHolder.tv2.setTypeface(viewHolder.tv2.getTypeface(), Typeface.BOLD);
                viewHolder.tv3.setTypeface(viewHolder.tv3.getTypeface(), Typeface.BOLD);
            }

            return view;
        }


When the ListView reuses a view, it may have been bold before scrolling and should be plain now. Try explicitly setting the typeface to PLAIN when the message is read.


You will have to pass null in setTypeFace() instead of using getTypeFace().

When getTypeFace() doesn't return null the style wont be set correctly.

// if message is unread, then make texts bold
if (messageRead_list[position] == false){
    viewHolder.tv1.setTypeface(null, Typeface.BOLD);
    viewHolder.tv2.setTypeface(null, Typeface.BOLD);
    viewHolder.tv3.setTypeface(null, Typeface.BOLD);
} else {
    viewHolder.tv1.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv2.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv3.setTypeface(null, Typeface.NORMAL);
}

Or you could use this directly which is what's done when you pass null.

setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

and

setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));


You should use two types of view counts by implementing getItemViewType(int position)

like this:

public int getViewTypeCount () {
    return 2;
}

and this:

public int getItemViewType (int position) {
    if (messageRead_list[position] == false) return 0;
    return 1;
}

and then checking the item type when you create them to set the proper typeface.

this is hack though, I'm setting the type to normal or default when on else as your comment should be the solution. Are you certain that messageRead_list is correct?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜