Java 动态添加的微调器为空
因此,我的应用程序中有一个动态添加和删除字段的活动,默认情况下,已有一个字段。原始微调器包含其中的所有信息,我可以选择其中的任何对象,但是,动态添加的微调器是空的。如何动态添加填充的微调器?我试着在addField方法中调用填充方法,但这并没有帮助,而且让事情变得更奇怪。下面是我添加新字段时的情况: 以下是添加字段的方法:Java 动态添加的微调器为空,java,android,android-layout,dynamic,spinner,Java,Android,Android Layout,Dynamic,Spinner,因此,我的应用程序中有一个动态添加和删除字段的活动,默认情况下,已有一个字段。原始微调器包含其中的所有信息,我可以选择其中的任何对象,但是,动态添加的微调器是空的。如何动态添加填充的微调器?我试着在addField方法中调用填充方法,但这并没有帮助,而且让事情变得更奇怪。下面是我添加新字段时的情况: 以下是添加字段的方法: public void addField(View v) { if(parentLayout.getChildCount() < maxCourses)
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();
}
}
这是新生成字段的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();
}
}
public void addField(视图五){
if(parentLayout.getChildCount()
为什么不使用适配器。如果使用适配器,可以从适配器列表中删除/添加项目。是否已将适配器添加到微调器中?请检查
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();
}
}