0

This is a bit tricky to explain, that's probably the reason why I haven't found any solution on the web so far...

I have a class which contains two variables - a String and a LinkedList of Strings:

String name;
LinkedList<String> stringlist;

stringlist normally holds between 1-4 Strings. Now I would like to create an Activity which shows the content of the objects from this class and shows EditText-fields dynamically, depending on the amount of values in the LinkedList.

I imagine it to look like this when there are 4 values in the list:

Title: <name>
=============
Value: <stringlist[0]>
Value: <stringlist[1]>
Value: <stringlist[2]>
Value: <stringlist[3]>

... and like this if there is only one value:

Title: <name>
=============
Value: <stringlist[0]>

How do I define my layout xml-file to be able to do this? Do I declare my "TextView-EditText-Combination" only once in my layoutfile and in my code to reprint this until it matches stringlist.length() ? Can I somehow do this repetition also for a whole layout-block?

Tongamann
  • 3
  • 4

1 Answers1

0
// try this way hope this will help you solve your problem.

my.xml

    <EditText
        android:id="@+id/edtTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Title"/>

    <LinearLayout
        android:id="@+id/lnrDynamicString"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

MyActivity

public class MyActivity extends Activity {

    My my;
    private EditText edtTitle;
    private LinearLayout lnrDynamicString;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my);

        edtTitle =(EditText) findViewById(R.id.edtTitle);
        lnrDynamicString=(LinearLayout)findViewById(R.id.lnrDynamicString);

        my = new My();
        my.setTitle("MyTitle");
        LinkedList<String> tempString = new LinkedList<String>();
        tempString.add("Child1");
        tempString.add("Child2");
        tempString.add("Child3");
        tempString.add("Child4");
        tempString.add("Child5");
        my.setStringlist(tempString);

        edtTitle.setText(my.getTitle());
        for (int i=0;i<my.getStringlist().size();i++){
            EditText edtChild = new EditText(this);
            edtChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            edtChild.setId(i);
            edtChild.setText(my.getStringlist().get(i));
            lnrDynamicString.addView(edtChild);
        }


    }

}

**My**
public class My {
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    private String title;

    public LinkedList<String> getStringlist() {
        return stringlist;
    }

    public void setStringlist(LinkedList<String> stringlist) {
        this.stringlist = stringlist;
    }

    private LinkedList<String> stringlist;
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Perfect - this was exactly what I was looking for. Thanks Haresh for the detailed explanation. That was really helpful for my understanding of this. – Tongamann Nov 07 '13 at 22:48