0

我已经为Android中的旋转器做了一个简单的演示,因为在第一个活动中进行了两个活动,在第二个活动中,第二个活动。以下代码但它不起作用:

Activity1.java

package com.example.sppiner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
Spinner sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp=(Spinner)findViewById(R.id.spinner1);
        String[] s={"ahmedabad","baroda","chitor","daman","england","france","georgia"};

        sp.setAdapter(new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,s));
        sp.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String n = (String)arg0.getItemAtPosition(arg2);
                Intent i =new Intent(MainActivity.this,Act2.class);
            Bundle b =new Bundle();
            i.putExtra("nat", n);
                startActivity(i);
            }

        });
    }
}

act2.java

  package com.example.sppiner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Act2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act2);
        TextView tv =(TextView)findViewById(R.id.tv1);

        Bundle b = getIntent().getExtras();
        Intent i =getIntent();
        String nat = b.getString("nat");
        tv.setText(nat);




    }



}

请帮助我...在此先感谢

4

3 回答 3

3

尝试setOnItemSelectedListener使用spinner

cat.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
String n = (String)arg0.getItemAtPosition(arg2);
Intent i =new Intent(MainActivity.this,Act2.class);
Bundle b =new Bundle();
i.putExtra("nat", n);
startActivity(i);
}

public void onNothingSelected(AdapterView<?> arg0) {

}
});

在第二个活动中

String city =b.getString("nat");

使用这条线代替

String city =b.getString("city");
于 2013-05-03T04:10:58.410 回答
1

在您的 Ist 活动中,执行以下操作:

sp.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            String n = (String)arg0.getItemAtPosition(arg2);
            Intent i =new Intent(MainActivity.this,Act2.class);
            i.putExtra("nat", n);
            startActivity(i);
        }

    });

并且,在您的第二个活动中,执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act2);
    TextView tv =(TextView)findViewById(R.id.tv1);
    String nat = getIntent().getStringExtra("nat");
    tv.setText(nat);

}
于 2013-05-03T04:10:57.333 回答
0

你能试试这个吗?

您需要通过key之前给出的值来获取值activity。IEnat

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activity2);
            tv=(TextView)findViewById(R.id.textView1);
            Intent i =getIntent();
            Bundle b =i.getExtras();
            String city =b.getStringExtras("nat");
            tv.setText(city);
        }
于 2013-05-03T03:59:06.923 回答