1
for (Element eHead : eHeadder) 
 {
String tag = eHead.tagName();
    if(tag.equals("p")
    {
     final String parsedText = eHead.text();
     Log.d("testing5", parsedText);
     runOnUiThread(new Runnable() 
       {
         public void run() { 
         data.setText(parsedText);
                            }    
        });
      }
  } 

In the above code, I get line of datas from "p"(paragraph) tag from an html page on the String variable "parsedText".

But my TextView named "data" is inside the for loop and i want to print each line of String, line by line in my TextView. Please help me with a solution.

2 Answers 2

2

You're setting the text each time instead of concatenating. It's also very inefficient using String like this in a loop. Instead, use a StringBuilder. Try this:

StringBuilder sb = new StringBuilder("");

for (Element eHead : eHeadder){
    if(eHead.tagName().equals("p"){
       sb.append(eHead.text());
       sb.append("\n");
       Log.d("testing5", ehead.text());
    } 
}

runOnUiThread(new Runnable(){
    public void run(){ 
        data.setText(sb.toString());                            }    
     });
}

This reduces the number of Strings created in the loop, reducing memory usage and increasing performance and also only sets the TextView text once. Much cleaner.

Sign up to request clarification or add additional context in comments.

Comments

0

Append \n with every parsed String your pulling and attach it to TextView.

 for (Element eHead : eHeadder) 
 {
 String tag = eHead.tagName();
 String addNextLine="";
   if(tag.equals("p")
    {
    final String parsedText = eHead.text();
     Log.d("testing5", parsedText);
     runOnUiThread(new Runnable() 
     {
      public void run() { 
       addNextLine = parsedText + "\n";
      data.setText(addNextLine);
                        }    
      });
   }
  } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.