【问题标题】:transfer data between 2 activities in a tab layout在选项卡布局中的 2 个活动之间传输数据
【发布时间】:2012-05-06 05:52:21
【问题描述】:

我正在实施一个健身应用。我有这个应用程序的选项卡布局,第一个选项卡显示位置(纬度、经度)、速度和其他状态,第二个选项卡显示谷歌地图,其中运行路线将显示为地图中的折线。

第一个标签活动有一个接收新位置的位置管理器。

我的问题是:如何将第一个选项卡活动中从位置管理器收到的数据传输到谷歌地图选项卡活动?

据我所知,通过使用 intent.putExtra() 和 startActivity() 可以在 2 个活动之间传输数据,但是通过调用 startActivity() 我会立即转到地图活动,对吗?但我想更新地图中的折线并停留在状态选项卡中。

有什么提示吗?

提前致谢:)

【问题讨论】:

    标签: android google-maps android-intent


    【解决方案1】:

    创建一个Global classdeclare static varible 并为其赋值并使用另一个类。 还有一种方式

    Intent i =new Intent()setClass(this, ListViewerIncompleted.class);
    i.putStringArrayListExtra(name, value);
    

    【讨论】:

      【解决方案2】:

      好的,我用那个代码解决了这个问题:

      在我的登录活动(第一个活动)中,我需要传递用户名和密码字符串:

      btnLogin.setOnClickListener(new OnClickListener(){
              public void onClick(View v) {               
                  String userName = txtUsername.getText().toString();
                  String password = txtPass.getText().toString();
                  //instance for UserFunctions.
                  UserFunctions userFunction = new UserFunctions();
                  JSONObject json = userFunction.loginUser(userName, password);
                  //Check login response
                  try {
                      if(json.getString(KEY_REQUESTRESULT)!= null ){
                          txtErrorM.setText("");
                          //Log.e("Passed fine:", "first if condition");
                          String res = json.getString(KEY_REQUESTRESULT);//Obtaining the value 0 or 1 from the KEY loginstatus from JSONObject.
                      if(Integer.parseInt(res) == 1){
                          Intent iii = new Intent("com.mariposatraining.courses.MariposaTrainingActivity");
                          Bundle bundle1 = new Bundle();
                          Bundle bundle2 = new Bundle();
                          bundle1.putString("UN", userName);
                          bundle2.putString("PW", password);
                          iii.putExtras(bundle1);
                          iii.putExtras(bundle2);
      
                          //iii.putExtra("userName", userName);
                          //iii.putExtra("Password", password);                       
                          startActivity(iii);
                          finish();
                          //Log.e("OBJECTOO", json.toString());
                      }
      

      我将此字符串发送到 TAB HANDLER CLASS,然后发送到管理此信息的活动:

      public class MariposaTrainingActivity extends TabActivity {
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE); 
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
          WindowManager.LayoutParams.FLAG_FULLSCREEN); 
          setContentView(R.layout.main);
      
          TabHost tabHost = getTabHost();
          TabHost.TabSpec spec ; //recurse to the property tabs
          Intent intent; //intent for open tabs
      
          //created intent for open the class tab1
      
          intent = new Intent().setClass(this, ListViewerIncompleted.class); //List 2 Incompleted
          spec = tabHost.newTabSpec("ListViewerIncompleted").setIndicator("INCOMPLETED").setContent(intent);
          tabHost.addTab(spec);
          Bundle bundle1 = getIntent().getExtras();
          String userName=bundle1.getString("UN");
          Bundle bundle2 = getIntent().getExtras();
          String password=bundle2.getString("PW");
          Bundle bundle3 = new Bundle();
          Bundle bundle4 = new Bundle();
          bundle3.putString("UN", userName);
          bundle4.putString("PW", password);
          intent.putExtras(bundle3);
          intent.putExtras(bundle4);}}
      

      在类中使用此信息:

       Bundle bundle3 = getIntent().getExtras();
          String userName=bundle3.getString("UN");   //Getting the userName
          Bundle bundle4 = getIntent().getExtras();
          String password=bundle4.getString("PW");
      

      希望这能给你一些想法......

      【讨论】:

        【解决方案3】:

        在您的目标 Activity 中使用公共静态变量,以便立即访问源 Activity 中所需的值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-31
          相关资源
          最近更新 更多