2015-04-24 56 views

回答

1

重播您可以使用相同的adapter爲您的紡紗廠,或者使用兩個適配器充滿了相同的元素。在後一種情況下,當您修改一個適配器時,您將需要使用notifyDataSetChanged()

0

活動代碼:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 

public class MainActivity extends Activity { 

    Spinner spinner1,spinner2; 

    private String[] numbers= {"One","Two","Three","Four","Five","Six", "Seven", "Eight","Nine", "Ten"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     spinner1=(Spinner)findViewById(R.id.spinner1); 
     spinner2=(Spinner)findViewById(R.id.spinner2); 

     ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, numbers); 

     spinner1.setAdapter(adapter_state); 
     spinner2.setAdapter(adapter_state); 

     spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
       spinner1.setSelection(position); 
       spinner2.setSelection(position); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 

     spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) { 
       spinner1.setSelection(position); 
       spinner2.setSelection(position); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 

    } 
} 

試試這個..希望它有助於..

相關問題