我正在膨胀Spinner
以及我inflating
其他views
LinearLayout child_linear = (LinearLayout) findViewById(R.id.child_linear);
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.add_custome_layout, child_linear, false);
name = (TextView) myView.findViewById(R.id.name);
price = (TextView) myView.findViewById(R.id.add_price);
names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
child_linear.addView(myView);
现在我获取了spinner
的数据。如果有'n'没有旋转器的问题我怎么知道用户选择了哪个微调器项目?
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
//Show PopUp or Alerts
Toast.makeText(ProductView.this, "position" + names_spinner.getItemIdAtPosition
(position), Toast.LENGTH_SHORT).show(); }
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
即使我点击特定的微调器项目位置也未显示在Toast
中。我的错是什么?
答案 0 :(得分:0)
试试这个
final Spinner names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
ArrayAdapter<String> madapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.yourarray));
madapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
names_spinner.setAdapter(madapter);
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String mySpinner = (String) adapterView.getItemAtPosition(i);
Log.i("names_spinner",mySpinner);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(getApplicationContext(),"Your Message",Toast.LENGTH_LONG).show();
}
});
}
答案 1 :(得分:0)
仅在用户单击“选择浇头”时才显示烤面包。看看你的代码:
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
更改此行
if (!parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
应该显示警报。
除了知道点击了哪个项目之外,听众还为您提供了点击的位置,您可以在这个位置搜索您的项目数组,并且您可以知道哪个项目。
编辑:
现在,假设您有多个微调器。例如,spinner1和spinner2。您可以为每个人注册一个OnItemSelectedListener
。
然后你可以创建一个名为Spinner clickedSpinner
的全局变量,在spinner1的OnItemSelectedListener
内你将clickedSpinner设置为spinner1,并在spinner2的OnItemSelectedListener
内设置clickedSpinner为spinner2:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner1;
... // Do your stuff here
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
和
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner2;
... // Do your stuff here
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
因此,点击任意Spinner
项中的任何项后,您现在可以知道点击了哪个Spinner
,因为它存储在clickedSpinner
中。
请注意,此变量不是必需的,您可以在没有此全局变量的情况下实现相同的行为,因为在每个OnItemSelectedListener
中您都知道单击了哪个微调器。所以,您可以执行以下操作:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner1Position = spinner1.getItemIdAtPosition(position);
... // Do your stuff here
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
和
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Position = spinner2.getItemIdAtPosition(position);
... // Do your stuff here
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
不使用全局变量。
希望这有帮助。