2014-10-28 61 views
0

我有4 Spinners和1 ButtonActivity A。我正在使用該Button打開另一個Activity B。現在我想在Activity B中顯示選定的Spinners信息。我知道如何通過EditText的數據,但我不知道Spinners將微調器信息傳遞給另一個活動

請幫我從另一個Activity

public class MainActivity extends ActionBarActivity { 

private Button btn; 
private Spinner spin1; 
private Spinner spin2; 
private Spinner spin3; 
private Spinner spin4; 


public class SpinnersData implements Serializable 
{ 
    private String spinner1; 
    private String spinner2; 
    private String spinner3; 
    private String spinner4; 


    public String getSpinner1() 
    { 
     return spinner1; 
    } 
    public void setSpinner1(String spinner1) 
    { 
     this.spinner1 = spinner1; 
    } 
    public String getSpinner2() 
    { 
     return spinner2; 
    } 
    public void setSpinner2(String spinner2) 
    { 
     this.spinner2 = spinner2; 
    } 
    public String getSpinner3() 
    { 
     return spinner3; 
    } 
    public void setSpinner3(String spinner3) 
    { 
     this.spinner3 = spinner3; 
    } 
    public String getSpinner4() 
    { 
     return spinner4; 
    } 
    public void setSpinner4(String spinner10) 
    { 
     this.spinner4 = spinner4; 
    } 
} 

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

    btn= (Button)findViewById(R.id.button1); 
    spin1=(Spinner)findViewById(R.id.spinner1); 
    final String arr1[] = {"NONE"," 1*£5.49","2*2.00","3*4.00"}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arr1); 
    spin1.setAdapter(adapter); 
    spin1.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(MainActivity.this, "the item selected "+arr1[arg2],Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
public void onNothingSelected(AdapterView<?> arg0) { 
      //TODO Auto-generated method stub 

     } 
    }); 
    spin1=(Spinner)findViewById(R.id.spinner1); 
    final String arr2[] = {"NONE"," 1*£5.49","2*2.00","3*4.00"}; 
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arr2); 
    spin2.setAdapter(adapter); 
    spin2.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(MainActivity.this, "the item selected "+arr2[arg2],Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
public void onNothingSelected(AdapterView<?> arg0) { 
      //TODO Auto-generated method stub 

     } 
    }); 
    spin3=(Spinner)findViewById(R.id.spinner1); 
    final String arr3[] = {"NONE"," 1*£5.49","2*2.00","3*4.00"}; 
    ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arr3); 
    spin3.setAdapter(adapter); 
    spin3.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(MainActivity.this, "the item selected "+arr3[arg2],Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
public void onNothingSelected(AdapterView<?> arg0) { 
      //TODO Auto-generated method stub 

     } 
    }); 
    spin4=(Spinner)findViewById(R.id.spinner1); 
    final String arr4[] = {"NONE"," 1*£5.49","2*2.00","3*4.00"}; 
    ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arr4); 
    spin4.setAdapter(adapter); 
    spin4.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(MainActivity.this, "the item selected "+arr4[arg2],Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
public void onNothingSelected(AdapterView<?> arg0) { 
      //TODO Auto-generated method stub 

     } 
    }); 

    btn= (Button)findViewById(R.id.button1); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent inte = new Intent (MainActivity.this, Finalorder.class); 
      SpinnersData data = new SpinnersData(); 
      data.setSpinner1(spin1.getSelectedItem().toString()); 
      data.setSpinner2(spin2.getSelectedItem().toString()); 
      data.setSpinner3(spin3.getSelectedItem().toString()); 
      data.setSpinner4(spin4.getSelectedItem().toString()); 

      inte.putExtra("SPINNERVAL", data); 
      startActivity(inte); 

     } 
    }); 
}} 

收到Spinners信息下面是第二Activity,我想收到Spinners信息。我不知道我在哪裏寫錯了代碼。請

活動2

public class Finalorder extends MainActivity { 

Bundle Bundle; 
TextView txt; 
Button btn; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.last); 

    Bundle=getIntent().getExtras(); 
    SpinnersData data = (SpinnersData) getIntent().getExtras().getSerializable("SPINNERVAL"); 

     }} 
+0

問題是什麼? – Blundell 2014-10-28 22:01:38

+0

隨機猜測:使'SpinnersData'類'靜態' – Blundell 2014-10-28 22:02:25

+0

我嘗試從藍色堆棧應用程序播放器。它沒有在那開放。我不知道我的代碼有什麼錯誤 – sudheir 2014-10-28 22:13:58

回答

1

可以實現擴展ArrayAdapter你的微調適配器並從適配器,如檢索​​數據。

 public void onItemSelected(AdapterView<?> parent, View view, 
      int pos, long id) { 
       String obj= myAdapter.getItem(pos);     
         data.setSpinner1(obj.get()); 

    } 

聲明適配器

ArrayList<String> list = new ArrayList<String>(); 
      list.add("1.Position ONE"); 
      list.add("2.Position TWO"); 
      list.add("3.Position THREE"); 
private MyAdapter myAdapter=new MyAdapter(getActivity(), 
           android.R.layout.simple_spinner_item,list); 
      spinner1.setAdapter(myAdapter); 
+0

與活動之間傳遞數據有什麼關係? – 2014-10-28 22:57:29

+0

data.setSpinner1(spin1.getSelectedItem()。toString()); //這是錯誤的 這就是爲什麼他什麼都沒有。 – 2014-10-28 23:44:00

+0

至少,.getSelectedItem()。toString()是從微調器中獲取所選項目字符串值的正確調用。所以這個電話本身並不是它沒有得到任何東西的原因。 – 2014-10-28 23:49:56

相關問題