如何知道旋转器中所选项目的位置?

时间:2018-05-12 07:02:16

标签: android

我正在膨胀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中。我的错是什么?

2 个答案:

答案 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) {

  }
});

不使用全局变量。

希望这有帮助。