-1

I am using layout inflator to generator dynamic text views, edit text and buttons in android. Everything is working fine but I am getting an extra button, edit text and text view . I am populating the value of text views dynamically and it too starts populating from second text view. Here is my code. My xml code,

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textviewid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />

        <EditText
            android:id="@+id/edittextid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayoutid"
        android:layout_centerHorizontal="true"
        android:text="button" />

</LinearLayout>

My code in for populating values dynamically,

   setContentView(R.layout.activity_main);
   LinearLayout lv = (LinearLayout) findViewById(R.id.linearlayoutid);
    for (int i = 0; i < 41; i++) {
         TextView[] myTextViews = new TextView[41];
         EditText[] editTextarray = new EditText[41];
        View v = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        TextView tv= (TextView) v.findViewById(R.id.textviewid);
        EditText editText = (EditText) v.findViewById(R.id.edittextid);
        myTextViews[i] = tv;
        myTextViews[i].setText(d.get(i));
        editTextarray[i] = editText;
        editText.setCursorVisible(true);
        lv.addView(v);
    }

I am getting an additional textview, edittext and button in layout. I must get only 41 but I am getting 42 with first one being "empty" or no values populated. Any help would be glad. ! Thanks

Anusha
  • 939
  • 3
  • 13
  • 31
  • You really should have a look at ListView or RecyclerView, those were made to deal with problems like yours. – ElDuderino May 04 '16 at 15:27
  • Can you find what's the problem here @ElDuderino – Anusha May 04 '16 at 15:36
  • Yes. You are setting the xml file as content view for your activity. So there is a textview, a button and an edittext already there. Then you start inflating THE SAME xml file, and adding it to the linearlayout of your contentview, setting the values of the widgets. So yeah with each inflation a new textview, button and edittext are added, but there is still the one that was originally there when you set it as contentview. – ElDuderino May 04 '16 at 15:45
  • How should I resolve it? @ElDuderino – Anusha May 04 '16 at 15:46
  • Um, follow my first comment? It is the best way. Or just use a simple LinearLayout as contentView without the other stuff in the xml. – ElDuderino May 04 '16 at 15:49
  • Using ListView will make it complicated but if I remove other stuffs in xml how would I use it in my code @ElDuderino – Anusha May 04 '16 at 15:51

1 Answers1

0

You have to create 2 xml files, one for your activity, one for the view that you inflate.

for activity_main.xml

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


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


    </LinearLayout>

</ScrollView>

for myview.xml

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textviewid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />

        <EditText
            android:id="@+id/edittextid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayoutid"
        android:layout_centerHorizontal="true"
        android:text="button" />

</LinearLayout>

The code

setContentView(R.layout.activity_main);
LinearLayout lv = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 41; i++) {
         TextView[] myTextViews = new TextView[41];
         EditText[] editTextarray = new EditText[41];
         View v = LayoutInflater.from(this).inflate(R.layout.myview, null);
         TextView tv= (TextView) v.findViewById(R.id.textviewid);
         EditText editText = (EditText) v.findViewById(R.id.edittextid);
         myTextViews[i] = tv;
         myTextViews[i].setText(d.get(i));
         editTextarray[i] = editText;
         editText.setCursorVisible(true);
         lv.addView(v);
}

But I'll say it again...use RecyclerView.

ElDuderino
  • 3,253
  • 2
  • 21
  • 38