I have a ListView where I managed to display many texts on it. Some of the texts are formatted as BOLD. In order to make those texts bold, I used Spannable and it works! However, when I scrolled it down and up again, the index[0] goes multiple.
To avoid that scene, I used a holder to hold my TextViews. I managed to solve that problem in scrolling but for some reasons, the textstyle which is BOLD, gone in my ListView. How can I do that?
MainActivity.java
private void populateListView() {
String[] source = { "Lorem ipsum dolor sit amet", "consectetuer adipiscing elit",
"sed diam nonummy nibh " };
final SpannableString out0 = new SpannableString(source[0]);
final SpannableString out2 = new SpannableString(source[2]);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
out0.setSpan(boldSpan, 6, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out2.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.items, // Layout to use (create)
source) { // Items to be displayed
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
try {
viewHolder.tv = (TextView) lv_procedures.getChildAt(0)
.findViewById(R.id.lbl_item);
viewHolder.tv_2 = (TextView) lv_procedures.getChildAt(2)
.findViewById(R.id.lbl_item);
viewHolder.tv.setText(out0);
viewHolder.tv_2.setText(out2);
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
};
}
Class ViewHolder
private class ViewHolder {
TextView tv;
TextView tv_2;
}
Create custom adapter and implement ViewHolder pattern to maintain List each item state:
class CustomAdapter extends ArrayAdapter<String>{
private Context context;
private String[] source;
public CustomAdapter(Context context, String[] source) {
super(context,R.layout.items,source);
this.context = context;
this.source = source;
}
@Override
public int getCount() {
return source.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.items,null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.lbl_item);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if(position==0){
final SpannableString out0 = new SpannableString(source[position]);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
out0.setSpan(boldSpan, 6, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.tv.setText(out0);
}else if(position==2){
final SpannableString out2 = new SpannableString(source[position]);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
out2.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.tv.setText(out2);
}else{
holder.tv.setText(source[position]);
}
convertView.setTag(holder);
return convertView;
}
private class ViewHolder {
TextView tv;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With