Rathina Sabapathi M Rathina Sabapathi M - 5 months ago 53
Android Question

how to get value from custom child view in parent view in android

Hi guys I am integrating an app with dynamic child view in a parent View.

My code to adding child view in parent view XML file.

<ScrollView 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"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add_view"
android:orientation="vertical">
</LinearLayout>
</ScrollView>


This is my code for add child view in Activity.

for (int i = 0;i < BloodInstance.getInstance().tempBloodResultDetails.size();i++){
View view = LayoutInflater.from(this).inflate(R.layout.child_view, null);
BloodResultDetails details = BloodInstance.getInstance().tempBloodResultDetails.get(i);
resultName = (TextView) view.findViewById(R.id.child_name);
resultRange = (TextView) view.findViewById(R.id.child_range);
resultGetRange = (EditText) view.findViewById(R.id.child_get_range);

resultName.setText(details.getName());
resultRange.setText("Normal Range "+details.getMinRange()+" to "+details.getMaxRange()+" "+details.getUnit());

addLayout.addView(view);
}


Here is my child view XML file.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:background="#e1e1e1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/child_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center"
android:textColor="#000"
android:textSize="20dp"
android:typeface="serif"/>
<TextView
android:id="@+id/child_range"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Normal Range"
android:gravity="center"/>
<EditText
android:id="@+id/child_get_range"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Your Range"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center"
android:background="@drawable/box"/>

</LinearLayout>
</android.support.v7.widget.CardView>


I have use this code to save the values.

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0;i < BloodInstance.getInstance().tempBloodResultDetails.size();i++){
String result = resultGetRange.getText().toString();
Log.e("Result",result);
resultUserRange.add(result);
}
}
});


How to get value from EditText for each child view.

Answer

You could give an ID to the View:

View view = LayoutInflater.from(this).inflate(R.layout.child_view, null);
int id = View.generateViewId(); // store this ID somewhere
view.setId(id);

After that you could do something like this to get the text from your EditText:

View parent = findViewById(id);
EditText child = (EditText) parent.findViewById(R.id.child_get_range);
String text = child.getText().toString();