0

Possible Duplicate:
How to clear/empty Java Stringbuilder

I'm adding values to a string and then displaying it in a text view, but when I refresh the page it doesn't clear the string first.
It just keeps adding to it.

Is there a way to clear a string builder in android?

What I have tried so far

StringBuilder sb = new StringBuilder();
sb.delete(0,sb.length());
for (String s : awayScores)
{
    sb.append(s);
    sb.append("\n");
}

StringBuilder sb2 = new StringBuilder();
sb2.delete(0,sb2.length());
for (String h : homeScores)
{
    sb2.append(h);
    sb2.append("\n");
}

 String awayeve = sb.toString();
 String homeeve = sb2.toString();

Log.v("home", "homeeve" + homeeve);
Log.v("home", "awayeve" + awayeve);
Community
  • 1
  • 1
Luke Batley
  • 2,384
  • 10
  • 44
  • 76
  • what do you mean by refresh the page? How are you doing this? – Andro Selva Jul 24 '12 at 08:35
  • i have a refresh button that runs an asynctask again. i just want to know how to clear the string builder when that task runs so that each time it'll only display the values once – Luke Batley Jul 24 '12 at 08:37

2 Answers2

5

Try using sb.delete(0,sb.length());

EDIT:

StringBuilder sb2 = new StringBuilder();
sb2.delete(0,sb2.length());

In the code above, you are trying to delete contents of an empty StringBuilder.

This can't be done.

Matthew
  • 1,905
  • 3
  • 19
  • 26
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • where shall i put it i have edited my code above but this still doesn't work – Luke Batley Jul 24 '12 at 08:53
  • When you find need to clear stringBuilder contents, you can call this method with SB object. – AAnkit Jul 24 '12 at 08:56
  • You are deleting content and then again appending in the loop. how it gonna work den!!.. what do you exactly want. – AAnkit Jul 24 '12 at 09:13
  • sorry i have found out where i was going wrong it was to do with array lists i was putting into the string builder it was them i needed to clear not the string builder but thank you anyway useful to know – Luke Batley Jul 24 '12 at 09:32
0

you can use setText(""); it will reset value with blank string

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37