Reputation: 13
The EditText is Dynamically added by user . how get data from each dynamically created EditText .or get values from the dynamically created EditTexts when the user tap a save button, then store all of them , dynamically added by user and save as Share preferences or save as array thanks guys
public class ActivityOptions extends AppCompatActivity {
private LinearLayout parentLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="text"/>
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
<Button
android:id="@+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:layout_gravity="center"
android:onClick="onAddField"
android:textColor="#FFF"
android:text="Add Field"
android:paddingLeft="5dp"/>
<TextView
android:layout_marginTop="10dp"
android:gravity="center"
android:id="@+id/txt"
android:text="HELLO"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
fiel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="text"/>
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
Upvotes: 0
Views: 962
Reputation: 36
You could retrieve the value by looping the parentView, something like this
ArrayList getEditTextList()
{
ArrayList<String> list = new ArrayList<String>();
int size = parentLinearLayout.getChildCount()
for (int i=0 ; i < size ; i++)
{
View view = parentLinearLayout.getChildAt(i);
EditText text = view.findViewById(R.id.yourEditTextName);
list.add(text.getText().toString());
}
return list;
}
Later you can add the values to Shared preference either by converting Arraylist to Json String or Adding it one by one.
Upvotes: 2