0

First, here is my code :

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linLayout = new LinearLayout(this); 

        TextView text=new TextView(this);
        ArrayList<String> recup = recupData();
        List<TextView> lvariables = new ArrayList<TextView>();
        int i = 0;

        for(i=0;i<recup.size();i++){
            text.setText(recup.get(i)+"\n");
            lvariables.add(text);
            linLayout.addView(lvariables.get(i));
        }
        setContentView(linLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

Now, my question is : How to display the N textviews (generated in the loop and stored in my List) on the same layout (one under another for instance) !?

With the code you can see, I receive an "IllegalState" Exception !

Here's the XML file, just in case :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
</RelativeLayout>

(Short isn't it !? :/ )

I'm conscient that this question is basic and stupid but I can't get my brain to understand the way android philosophy works !

If someone is patient enough to help me with my issue, even if it has certainly been discussed a million time on StackOverFlow, I would be really grateful !

I'd really appreciate any help you can provide ! Oh and please, excuse my english, I'm french... :D

1 Answers1

2

you are adding always the same TextView wich is not allowed

Change:

for(i=0;i<recup.size();i++){
      text.setText(recup.get(i)+"\n");
      lvariables.add(text);
      linLayout.addView(lvariables.get(i));
}

with

for(i=0;i<recup.size();i++){
         TextView text=new TextView(this);
        text.setText(recup.get(i)+"\n");
        lvariables.add(text);
        linLayout.addView(text);
 }

Edit: To change the orientation of your LinearLayout to vertical:

 LinearLayout linLayout = new LinearLayout(this); 
 linLayout.setOrientation(LinearLayout.VERTICAL);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305