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.
- the top item is always BOLD no matter it is read or unread.
- 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 = convertView;
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;
}
You will have to pass
nullinsetTypeFace()instead of usinggetTypeFace().When
getTypeFace()doesn’t returnnullthe style wont be set correctly.Or you could use this directly which is what’s done when you pass
null.and