将Spinner值传递给下一个活动

时间:2014-11-19 19:28:58

标签: java android spinner

First Activity需要将微调器结果发送到第二个活动以供显示。我一直得到android.widget.Spinner {b2e1e158 VFED..C .........我。 0,0-100,37#7f050045 app:id / spinner1} 我还得早点发布数组,但我无法回复。

    Spinner spinner1;
String table;
List<String> list;

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

    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    list = new ArrayList<String>();
    list.add("Table 1");
    list.add("Table 2");
    list.add("Table 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            spinner1.getSelectedItem().toString();


        }

        public void onNothingSelected(AdapterView<?> arg0) {


        }
    });

    //startButton is connected to button1 on the screen
    Button startButton = (Button) findViewById(R.id.button1); 
    //Listens for when the start button is clicked
    startButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        { 
            //Calls the secondActivity method
            thirdActivity();
        } 
    });
}
private void thirdActivity() 
{ 
    //Tell intent which activity to go from and to
    Intent launch = new Intent(this, MainActivity2.class);
    launch.putExtra("list", list.toString());
startActivity(launch);
}

第二项活动

private String list;
private TextView displayTable;
 Intent launch = getIntent();
    list = launch.getStringExtra("list");
    displayTable = (TextView) findViewById(R.id.textView6);
    displayTable.setText(list);

1 个答案:

答案 0 :(得分:0)

试试这个

Spinner spinner1;
String table;
List<String> list;
String selectedItem="";

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

toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

spinner1 = (Spinner) findViewById(R.id.spinner1);
list = new ArrayList<String>();
list.add("Table 1");
list.add("Table 2");
list.add("Table 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        selectedItem=spinner1.getSelectedItem().toString();


    }

    public void onNothingSelected(AdapterView<?> arg0) {


    }
});

//startButton is connected to button1 on the screen
Button startButton = (Button) findViewById(R.id.button1); 
//Listens for when the start button is clicked
startButton.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View view) 
    { 
        //Calls the secondActivity method
        //thirdActivity();

     Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
     intent.putExtra("selectedItem",selectedItem);
     startActivity(intent);


    } 
});

}

第二项活动

Intent launch = getIntent();
list = launch.getStringExtra("selectedItem");
displayTable = (TextView) findViewById(R.id.textView6);
displayTable.setText(list);