从一个活动向另一个活动发送和接收选定的微调器信息

时间:2014-10-28 19:45:50

标签: java android

我有10个微调器和一个按钮。通过单击按钮活动A将转到另一个活动B,我想通过单击该按钮从活动A添加所选信息以从活动A发送到活动B,并希望在活动B中显示选定的微调器信息。

这是我的代码(活动A):

public class Firstactivity extends MainActivity {

    private Button btn;
    private Spinner spin;

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

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

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

            }

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

            }
        });


        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent inte = new Intent (Firstactivity.this, Finalorder.class);
                String Text = spin.getSelectedItem().toString();
                startActivity(inte);

            }
        });
}}

活动B:

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();
        btn=(Button)findViewById(R.id.btn1s);


        Bundle=getIntent().getExtras();
        txt.setText(Bundle.getString("SPINNERVAL"));

    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用所有微调器值创建自定义可序列化或可分割对象。并通过活动进行转移。

类似的东西:

public class SpinnersData implements Serializable
{
    private String spinner1;
    private String spinner2;
    //....
    private String spinner10;


    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 getSpinner10()
    {
        return spinner10;
    }
    public void setSpinner10(String spinner10)
    {
        this.spinner10 = spinner10;
    }
}

将其用于一项活动:

SpinnersData data = new SpinnersData();
data.setSpinner1(spinner1.getSelectedItem().toString());
data.setSpinner2(spinner2.getSelectedItem().toString());
....
yourIntent.putExtra("someKey", data);

并参加onCreate()中的其他活动:

SpinnersData data = (SpinnersData) getIntent().getExtras().getSerializable("someKey");

现在您可以使用此对象在此处显示信息。