刚学的 activity 之间的两种(带数据)跳转

 

    
    activity 之间的跳转
    
    1.普通跳转
                //在监听器中,如onClickListener 方法中添加
                Intent intent = new Intent();
                intent.setClass(ActivityTestActivity.this, AnotherActivity.class);
                
                
                Bundle bundle = new Bundle();    //创建 bundle 存放对象
                bundle.putString("stringValue", testValue);
                intent.putExtras(bundle);    //把要带的数据放入intent
                
                startActivity(intent);
                
                ------
                
                //在另一个intent中取出数据:
                Intent intent = getIntent();    //获取启动该result 的intent
                Bundle bundle = intent.getExtras();
                String value = bundle.getString("cityValue");
                
    
    2.通过另一个activity 输入值 再跳回
                //在当前activity 中
                //在监听器中,如onClickListener 方法中添加
                Intent intent = new Intent(ActivityTestActivity.this,ThridActivity.class);
                startActivityForResult(intent, 1);    // 1 是请求码
                
                //重写回调方法 ,该方法在 另一个activity 中结束的时候    会自动调用 。
                @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

                    if(resultCode == 0 && requestCode == 1)
                    {
                        Bundle data = intent.getExtras();
                        String resultCity = data.getString("city");
                        EditText city = (EditText)findViewById(R.id.address);
                        city.setText(resultCity);
                    }
                }
                
            //这是 另一个activity 类的写法 。 里面有一个listView 提供给用户选择
                public class ThridActivity extends Activity {
                String[] citys = new String[]{
                        "beijing",
                        "shanghai",
                        "guangzhou"
                };
                 public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.third);
                        ListView cityListView = (ListView)findViewById(R.id.citys_listview);
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,citys);
                        cityListView.setAdapter(adapter);
                        
                        cityListView.setOnItemClickListener(new OnItemClickListener() {
            
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int selectedItem, long arg3) {
                                Intent intent = getIntent();
                                Bundle data = new Bundle();
                                data.putString("city", citys[selectedItem]);
                                intent.putExtras(data);
                                ThridActivity.this.setResult(0, intent);    //0 是结果码
                                ThridActivity.this.finish();
                                
                            }
                        });
                 }
                }
                
                

 

 

posted @ 2013-05-02 11:30  LaoQuans  阅读(299)  评论(0编辑  收藏  举报