I'm trying to print out a table using TextView and setText, but I can't get the table to print out correctly. It only prints out a single line. As I understand it setText overwrites the previous line? How can I get around this problem so that my for loop prints out every line instead of just overwriting the current line?
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Character \t Correct \t Incorrect \t Percentage\n");
ll.addView(tv);
for(int counter = 0; counter<scores1.length;counter++){
tv.setText(level1[counter] + "\t " + correct1[counter] + "\t " + incorrect1[counter] + "\t " + percentage1[counter] + "\n");
}
this.setContentView(sv);
You should consider using a ListView
, see
http://developer.android.com/guide/topics/ui/layout/listview.html
There's an example on the Android ListView
page and you can also check out:
http://www.vogella.com/articles/AndroidListView/article.html#listview_listviewexample
ListView
takes care of creating the TextView
s for you and re-uses them to save memory and offers adapters to use data from the database and other sources.
A quick hack for your code would be:
for(int counter = 0; counter<scores1.length;counter++){
tv = new TextView(this);
ll.addView(tv);
tv.setText(level1[counter] + "\t " + correct1[counter] + "\t " + incorrect1[counter] + "\t " + percentage1[counter] + "\n");
}