循环膨胀视图的奇怪小故障

时间:2015-07-18 13:49:13

标签: android for-loop layout-inflater

我正在尝试获取一个字符串(格式化为HTML)并在每次双输入时将其拆分。我知道在双重输入之前和之后将是一个p块标记,并且我正在使用for循环以这种方式将其拆分。这是我的代码

for(String s : rawHTML.split("\\n\\n")){
        View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
        LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
        TextView comm = (TextView) newView.findViewById(R.id.commentLine);

        Log.v("Slide", "Should be " + s );
        Log.v("Slide", "Currently is "+ comm.getText());
        comm.setText(Html.fromHtml(s) );

此预期行为是日志中的每个“当前是”应为空白。但是,如果有两个或更多p块,当我使用setText时,它会覆盖以前的文本。

例如,我有一个字符串

<p>Test</p>\n\n<p>Hello</p>

我应该看到两个像这样的TextView

[Test]

[Hello]

相反,我看到了这个

[Hello]

[]

我真的很难过,无法弄清楚这个奇怪的问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

以下代码段适合您。

   View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
   LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
   TextView comm = (TextView) newView.findViewById(R.id.commentLine);

   Log.v("Slide", "Should be " + s );
   Log.v("Slide", "Currently is "+ comm.getText());
   for(String s : rawHTML.split("\\n\\n")){
        comm.append(Html.fromHtml(s) );
   }