0
    int numberofSpinner = TransportResult.Transfers.size();
    Spinner spin=null;
    for(int i=0;i<numberofSpinner;i++)
    {
        spin = new Spinner(this);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,1);
        spinLayout.addView(spin,p);
        spin.setId(i);
        Transfer transfer = TransportResult.Transfers.get(i);
        ArrayList<CharSequence> s = new ArrayList<CharSequence>();
        for( Line l : transfer.TransferLine)
        {
            s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
        }
        adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(adapter);
    }
    spin.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                  int position, long id) {
           if(parent.getId()==0){
       System.out.println("spin 1 is called");
               String str = (String)parent.getSelectedItem(); 
         }else if(parent.getId()==1){
       System.out.println("spin 2 is called");
               String str = (String)parent.getSelectedItem();  
     }
         }   
      public void onNothingSelected(AdapterView<?> arg0) {

          }
      });

如果微调器的数量大于 1,则仅触发最后一个微调器。例如; 我在屏幕上有 3 个微调器,当我选择第一个或第二个微调器的项目时,永远不会触发侦听器。只有第三个微调器会触发侦听器。我该如何解决?

谢谢

4

1 回答 1

1

更新

当您使用多个微调器时,然后为每个微调器设置 ID。您可以在记住第一次设置 OnitemSelected 时调用 onItemSelected 时spin.setId(int)检查 id 。OnItemSelected method.

 spin.setOnItemSelectedListener(new OnItemSelectedListener() {
              @Override
               public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {

                 if(parent.getId()==1){
             System.out.println("spin 1 is called");
                     String str = (String)parent.getSelectedItem(); 
               }else if(parent.getId()==2){
             System.out.println("spin 2 is called");
                     String str = (String)parent.getSelectedItem();  
           }
               }   
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

                }
            });
于 2012-06-08T17:31:38.140 回答