-2
String hallo = "blabla " + "jojo " + "\n" + "doj " + "drasl " +"\n";
InputStream is = new ByteArrayInputStream(hallo.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));

String reader;
    while ((reader = in.readLine()) != null) {
    String[] RowData = reader.split(" ");
    String eitt = RowData[0];
    String tvo = RowData[1];

I have a string with the same format as the hallo string, except I don't know how many lines there are. I'm trying to put the information in a table layout (or what ever is most convenient).

How do I automatically create the exact number of textview windows in the table layout as I need, and automatically input my string value?

By the way, the textview is inside a scrollview layout, if that matters.

gnat
  • 6,213
  • 108
  • 53
  • 73
davidhlynsson
  • 79
  • 1
  • 8

1 Answers1

0

Use the following code

String hallo = "blabla " + "jojo " + "\n" + "doj " + "drasl " + "\n";
InputStream is = new ByteArrayInputStream(hallo.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

String line;
try {
    while ((line = in.readLine()) != null) {
        String[] RowData = line.split(" ");
        String eitt = RowData[0];
        String tvo = RowData[1];

        TextView textView = new TextView(this);
        textView.setText(line);

        linearLayout.addView(textView);
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

with this xml layout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</ScrollView>
Terel
  • 3,857
  • 1
  • 25
  • 28
  • Thank you very much. But when i use the following code, this error pops up, cannot use "this" in a static context. Do you know what that means ? – davidhlynsson Nov 01 '12 at 12:09
  • From where class / method you try to call this code? You can also try to use "getApplicationContext()" instead of "this" – Terel Nov 01 '12 at 12:14
  • This works now with "getApplicationContext()" , thanks very much for the help. – davidhlynsson Nov 01 '12 at 12:28
  • I think you've misunderstood what I was going for. I want to insert these parts of a string into a table layout, so that when my pieces of text are printed they come out in nice columns. The way you did it just prints the string line for line instead of putting it into columns. It's also very important to know that I don't know how many lines are going to be in the string, so I can't just make 4 textview boxes because there could be 50 og a hundred lines, so I need to make an i amount of textview boxes in a table view with i being the number of lines. – davidhlynsson Nov 03 '12 at 11:40