5

我正在开发一个 Android 应用程序,我需要在 textView 中显示 HTLM(也是 < li> 标签)。为此,我找到了这个TagHandler但它引发了

java.lang.RuntimeException:PARAGRAPH span 必须从段落边界开始

我试图理解为什么,但我一直无法找到解决方案。我对Android很陌生,所以请帮助我。

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();

    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
            if (opening) {
                mListParents.add(tag);
            } else mListParents.remove(tag);

            mListItemCount = 0;
        } else if (tag.equals("li") && !opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {
        if (mListParents.lastElement().equals("ul")) {
            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
        } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount++;

            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.insert(start, mListItemCount + ". ");
            output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
        }
    }
}
4

1 回答 1

9

if(start<output.length())发现错误,之前加这个就简单解决了

output.setSpan()

正确且有效的代码是这样的:

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();

    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
            if (opening) {
                mListParents.add(tag);
            } else mListParents.remove(tag);

            mListItemCount = 0;
        } else if (tag.equals("li") && !opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {
        if (mListParents.lastElement().equals("ul")) {
            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            if(start<output.length())
                output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
        } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount++;

            output.append("\n");
            String[] split = output.toString().split("\n");

            int lastIndex = split.length - 1;
            int start = output.length() - split[lastIndex].length() - 1;
            output.insert(start, mListItemCount + ". ");
            if(start<output.length())
               output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
        }
    }
    }

更新:

在上面的代码遇到一些其他问题之后,我开发了这段代码,它可以毫无例外地工作,我希望它会有用。

public class HtmlTagHandler implements Html.TagHandler {
    private int mListItemCount = 0;
    private Vector<String> mListParents = new Vector<String>();
    private Vector<Integer> mListCounter = new Vector<Integer>();


    @Override
    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {

        if (tag.equals("ul") || tag.equals("ol") ) {
            if (opening) {
                mListParents.add(mListParents.size(),tag);
                mListCounter.add(mListCounter.size(),  0);

            } else {
                Log.d("Bohhh"," tolgo "+mListParents.get(mListParents.size()-1));
                mListParents.removeElementAt(mListParents.size()-1);
                Log.d("Bohhh","lunghezza "+mListParents.size());
                mListCounter.removeElementAt(mListCounter.size()-1);

            }

        } else if (tag.equals("li") && opening) {
            handleListTag(output);
        }



    }



    private void handleListTag(Editable output) {

        if (mListParents.lastElement().equals("ul")) {
           if(output.length()!=0)
            output.append("\n");
            for(int i=1;i<mListCounter.size();i++)
                output.append("\t");
            output.append("• ");
           } else if (mListParents.lastElement().equals("ol")) {
            mListItemCount=            mListCounter.lastElement()+1;
            if(output.length()!=0)
            output.append("\n");
            for(int i=1;i<mListCounter.size();i++)
                output.append("\t");
           output.append( mListItemCount + ". ");
           mListCounter.removeElementAt(mListCounter.size()-1);
           mListCounter.add(mListCounter.size(), mListItemCount);
      }
    }

}
于 2014-11-13T16:26:56.067 回答