如何从 AdapterView<?> 父项中提取 Spinner Layout id?
How can I extract Spinner Layout id from AdapterView<?> parent?
这是我的 onItemSelected 代码 spinner。
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " , Toast.LENGTH_LONG).show();
Log.d("Value",parent.toString());
}
当我打印 parent.tostring 时,它给了我这个:
android.support.v7.widget.AppCompatSpinner{9f46108 VFED..C.. ......ID 552,31-942,169 #7f0800ee app:id/**spinner_walk**}
我想从那里提取id/spinner_walk,有什么办法吗?
要提取设置了 onItemSelected 侦听器的微调器的 ID,您需要获取 adapterView 的 ID,即
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This will return the id of the selected spinner
int id = adapterView.getId();
if(id == R.id.spinner_walk){
Toast.makeToast(context, "Spinner Walk selected", Toast.LENGTH_SHORT).show();
}
}
这是我的 onItemSelected 代码 spinner。
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " , Toast.LENGTH_LONG).show();
Log.d("Value",parent.toString());
}
当我打印 parent.tostring 时,它给了我这个:
android.support.v7.widget.AppCompatSpinner{9f46108 VFED..C.. ......ID 552,31-942,169 #7f0800ee app:id/**spinner_walk**}
我想从那里提取id/spinner_walk,有什么办法吗?
要提取设置了 onItemSelected 侦听器的微调器的 ID,您需要获取 adapterView 的 ID,即
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This will return the id of the selected spinner
int id = adapterView.getId();
if(id == R.id.spinner_walk){
Toast.makeToast(context, "Spinner Walk selected", Toast.LENGTH_SHORT).show();
}
}