动态添加的微调器为空
Dynamically added spinners are empty
所以我的应用程序中有一个 activity 可以动态添加和删除字段,默认情况下,那里已经有一个字段。原始微调器包含其中的所有信息,我可以 select 那里的任何对象,但是,动态添加的微调器是空的。我将如何动态添加填充的微调器?我试着在 addField 方法中调用一个填充方法,但这没有帮助并且让事情变得更奇怪。这是我添加新字段时的样子:
这里是添加字段的方法:
public void addField(View v) {
if(parentLayout.getChildCount() < maxCourses) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
//add new row before add field button
parentLayout.addView(rowView, parentLayout.getChildCount() - 1);
} else {
Toast.makeText(this, "No more than " + maxCourses + " courses\ncan be taken per semester", Toast.LENGTH_SHORT).show();
}
}
这是我通过按钮调用它的方式 "ADD COURSE"
addFieldBtn = findViewById(R.id.add_field_btn);
addFieldBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addField(v);
}
});
这是新生成字段的 field.XML 代码,它们与原始填充字段共享相同的 ID
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorSecondaryDark"
android:layout_alignParentBottom="true">
<Spinner
android:id="@+id/first_spinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/remove_field_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorSecondary"
android:gravity="center"
android:onClick="removeField"
android:text="REMOVE\nCOURSE"
android:textSize="12sp" />
</LinearLayout>
如果有人能给我一些提示或帮助,我将不胜感激!
新的微调器不知道从哪里获取数据。您应该为每个新微调器设置一个适配器。
喜欢这里:
public void addField(View v) {
if(parentLayout.getChildCount() < maxCourses) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
Spinner spinner = rowView.findViewById(R.id.first_spinner);
ArrayAdapter<String> arrayAdapter = getAnAdapterForThatSpinner();
spinner.setAdapter(arrayAdapter)
//add new row before add field button
parentLayout.addView(rowView);
} else {
Toast.makeText(this, "No more than " + maxCourses + " courses\ncan be taken per semester", Toast.LENGTH_SHORT).show();
}
}
所以我的应用程序中有一个 activity 可以动态添加和删除字段,默认情况下,那里已经有一个字段。原始微调器包含其中的所有信息,我可以 select 那里的任何对象,但是,动态添加的微调器是空的。我将如何动态添加填充的微调器?我试着在 addField 方法中调用一个填充方法,但这没有帮助并且让事情变得更奇怪。这是我添加新字段时的样子:
这里是添加字段的方法:
public void addField(View v) {
if(parentLayout.getChildCount() < maxCourses) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
//add new row before add field button
parentLayout.addView(rowView, parentLayout.getChildCount() - 1);
} else {
Toast.makeText(this, "No more than " + maxCourses + " courses\ncan be taken per semester", Toast.LENGTH_SHORT).show();
}
}
这是我通过按钮调用它的方式 "ADD COURSE"
addFieldBtn = findViewById(R.id.add_field_btn);
addFieldBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addField(v);
}
});
这是新生成字段的 field.XML 代码,它们与原始填充字段共享相同的 ID
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorSecondaryDark"
android:layout_alignParentBottom="true">
<Spinner
android:id="@+id/first_spinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/remove_field_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorSecondary"
android:gravity="center"
android:onClick="removeField"
android:text="REMOVE\nCOURSE"
android:textSize="12sp" />
</LinearLayout>
如果有人能给我一些提示或帮助,我将不胜感激!
新的微调器不知道从哪里获取数据。您应该为每个新微调器设置一个适配器。 喜欢这里:
public void addField(View v) {
if(parentLayout.getChildCount() < maxCourses) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
Spinner spinner = rowView.findViewById(R.id.first_spinner);
ArrayAdapter<String> arrayAdapter = getAnAdapterForThatSpinner();
spinner.setAdapter(arrayAdapter)
//add new row before add field button
parentLayout.addView(rowView);
} else {
Toast.makeText(this, "No more than " + maxCourses + " courses\ncan be taken per semester", Toast.LENGTH_SHORT).show();
}
}