0

I am extending the ListView class so that I can make some of the text bold in the dropdown items. I am trying to Override the addView method so that i can change the text before the views are added. I am getting an error saying "method does not override method from it's superclass." addView is inherited from the class ViewGroup.

public class EditableListView extends ListView {

    public EditableListView(Context context){
        super(context);
    }
    public EditableListView(Context context, AttributeSet attrs){
        super(context, attrs);
    }
    public EditableListView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }


    @Override
    public void addView(View child, int index, LayoutParams params) {
        final SpannableStringBuilder builder = new SpannableStringBuilder(((CheckedTextView)child).getText().toString());
        final StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD);
        builder.setSpan(bold, 10, 21, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(bold, 36, 37, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        ((CheckedTextView)child).setText(builder);
        super.addView(child, index, params);

    }
}
Anthony Porter
  • 152
  • 1
  • 9

2 Answers2

1

Within the android api, it says that that method is unsupported and throws an exception when called.

http://developer.android.com/reference/android/widget/ListView.html

I'm not familiar with working with listviews, so I may have something wrong here. I see that ViewGroup's addView method is functional, but since ListView is a child very far down the line of children for whom addView throws an exception, what I think is happening is that it's overriding the parent method directly above it, in this case AbsListView, and seeing that that method is unsupported and thus throwing an exception.

For an explanation of why this is happening: Why is super.super.method(); not allowed in Java?

Community
  • 1
  • 1
JBires
  • 441
  • 3
  • 11
  • I noticed that it throws an exception also. Guess I'll have to find another way to access the children before they get added – Anthony Porter Jul 10 '14 at 02:01
0

If you have a class for your custom list view, then simply add the method you provided in the class you handle your custom list view, OR add the contents of the provided method in your class if you are already overriding the 'addView' method.

Steven
  • 271
  • 1
  • 8