将微调器信息传递给另一个活动

时间:2014-10-28 21:57:23

标签: java android

我在Spinners中有4 Button和1 Activity A。我正在使用Button打开另一个Activity B。现在,我想在Spinners中显示所选的Activity B信息。我知道如何从EditText传递数据,但我不知道Spinners

请帮助我从其他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);

        }
    });
}}

以下是我尝试接收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");

        }}

1 个答案:

答案 0 :(得分:1)

您可以实现一个适用于Spinner的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);