Search code examples
androidlayout-inflater

Save value from edittexts inside layoutinfaltter


I am using layoutinflatter to show multiple edittexts and textviews on a button click... How can i handle the values inside the edittetxts and textviews..? My code..

 buttonAdd.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater =
                    (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.row, null);
            TextView textOut = (TextView)addView.findViewById(R.id.textout);
            EditText val=(EditText)addView.findViewById(R.id.val);
            textOut.setText(textIn.getText().toString());
            Button buttonRemove = (Button)addView.findViewById(R.id.remove);
            buttonRemove.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    ((LinearLayout)addView.getParent()).removeView(addView);
                }});

            container.addView(addView);
        }
    });

row.xml

<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="wrap_content">

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

    <TextView
        android:id="@+id/textout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/textout"
        android:layout_weight="1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/val"
        android:layout_weight="1"
        android:hint="Value" />

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Remove"
        />
</LinearLayout>

I need to insert the values in database as textview(textout) as column name and edittext(val) as value... How can i resolve this issue.?


Solution

  • as your trying to add row on layout, you are creating object like this

    final View addView = layoutInflater.inflate(R.layout.row, null);
    

    now just Create a ArrayList, in which you can add this view like this

    list.add(addView);
    

    Now at the end, when you want to submit data just iterate all the views, and get its sub elements using

    ((TextView)addView.findViewById(<your text view's id>)).getText();
    

    and save it into you database, or any other place where you wanna use it.