英文:
How do I capture the value of a Material drop down menu selection in Java?
问题
我正在使用Material在Android Studio中使用Java设计一个带有下拉列表的表单。
我想要将用户从下拉列表中选择的内容保存到一个字符串中,以便我可以将其保存到Firebase,但是当我尝试记录结果时,它说选择为空。
有人知道我如何捕获用户从这种类型的下拉菜单中选择的内容吗?我之前有一个Spinner,它起作用,但是似乎不以相同的方式工作。
我的XML代码如下:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/cpdTypeLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Activity Type">
<AutoCompleteTextView
android:id="@+id/cpdType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
我的AddAdactivity.java代码如下:
// CPD TYPE DROPDOWN MENU
cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
cpdType = findViewById(R.id.cpdType);
String[] type = new String[]{
"Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
};
ArrayAdapter<String> adapterType = new ArrayAdapter<>(
AddActivity.this,
R.layout.dropdown_item,
type
);
cpdType.setAdapter(adapterType);
cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedTypeResult = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Log.d("TAG", "You have selected " + selectedTypeResult);
编辑:之后尝试了以下方法,但没有成功。
((AutoCompleteTextView) cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapterType.getItem(position);
String typeItem = ((AutoCompleteTextView) cpdTypeLayout.getEditText()).getText().toString();
Log.d("TAG", "selected type is:" + typeItem);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
请注意,这些代码块是用于处理下拉列表选择的,您可以根据需要进行调整。
英文:
I am using Material to design a form with drop down lists in android studio using Java.
I want to save the selection the user makes from the drop down list to a string so I can save this to Firebase, but when I try and log the result, it says the selection is null.
Does anyone know how I can capture what the user selects from this type of drop down menu? I did previously have a Spinner which worked but this doesn't seem to work in the same manner.
My XML code is as follows
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/cpdTypeLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Activity Type">
<AutoCompleteTextView
android:id="@+id/cpdType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
My AddAdactivity.java code is
//CPD TYPE DROPDOWN MENU
cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
cpdType = findViewById(R.id.cpdType);
String[] type = new String[]{
"Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
};
ArrayAdapter<String> adapterType = new ArrayAdapter<>(
AddActivity.this,
R.layout.dropdown_item,
type
);
cpdType.setAdapter(adapterType);
cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedTypeResult = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Log.d("TAG", "You have selected " + selectedTypeResult);
EDIT: have since tried this but with no luck.
((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapterType.getItem(position);
String typeItem = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
Log.d("TAG", "selected type is:" + typeItem);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案1
得分: 16
获取String
只需使用以下代码:
String selectedValue = ((AutoCompleteTextView) textInputLayout.getEditText()).getText();
如果你想使用监听器,可以使用类似以下代码:
((AutoCompleteTextView) textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = arrayAdapter.getItem(position);
}
});
英文:
To get the String
you can just use:
String selectedValue =((AutoCompleteTextView)textInputLayout.getEditText()).getText();
Otherwise if you want to use a listener you can use something like:
((AutoCompleteTextView)textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = arrayAdapter.getItem(position);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论