[英]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
. 嗨,大家好,我正在将应用与动态子View
集成到父View
。
My code to adding child View
in parent view XML file. 我在父视图XML文件中添加子View
代码。
<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
. 这是我在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. 这是我的孩子的View
XML文件。
<?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
. 如何从EditText
获取每个子View
。
You could give an ID to the View
: 您可以给View
指定一个ID:
View view = LayoutInflater.from(this).inflate(R.layout.child_view, null);
int id = View.generateViewId();
view.setId(id);
After that you could do something like this to get the text from your EditText
: 之后,您可以执行以下操作从EditText
获取文本:
View parent = findViewById(id);
EditText child = (EditText) parent.findViewById(R.id.child_get_range);
String text = child.getText().toString();
Simply use the for-loop for addLayout(parent view) to get the child view, like. 只需对addLayout(parent view)使用for循环即可获得子视图,例如。
for(int ij=0;ij<addLayout.getChildCount();ij++)
{
//get user view here
}
then set id for views in add views like that view.setId(id);
然后在类似view.setId(id);
添加视图中设置视图的view.setId(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.