2013-07-26 46 views
0

我会喜欢一些帮助(对于Android来说是新鲜的)。在多个活动之间传递信息

我目前有4项活动。前三项活动中都有EditText字段,然后最后一项活动显示所有已输入的信息。似乎很简单,对吧?

因此,在查看本网站后,我发现这样做的方法是使用putExtra()和getExtra();

这似乎不太合适,因为我添加到额外哈希集中的所有内容在最终活动中都不可用。有什么建议?

感谢

下面

代码帮助

package com.example.smallchangebigloss; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class EnterName extends Activity { 
    private EditText edit; 
    private Bundle extras = getIntent().getExtras(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_enter_name); 
     edit = (EditText) findViewById(R.id.nameEdit); 

    } 

    public Bundle getExtras(){ 
     return extras; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.enter_name, menu); 
    return true; 
} 

public void next(View view) { 
    if (edit.getText().toString().equals("")) { 
     new AlertDialog.Builder(this).setTitle("Ut Oh!") 
       .setMessage("Please enter your name.") 
       .setNeutralButton("Try again", null).show(); 
    } 
    else { 
     Intent i = new Intent(getApplicationContext(), CurrentWeight.class); 
     i.putExtra("name", edit.getText().toString()); 
     startActivity(i); 
    } 
} 

}

package com.example.smallchangebigloss; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

公共类CurrentWeight延伸活动{

private EditText edit; 
private String name; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_current_weight); 
    Bundle extras = getIntent().getExtras(); 
    name = extras.getString("name"); 
    edit = (EditText) findViewById(R.id.currentWeightEdit); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.current_weight, menu); 
    return true; 
} 

public void next(View view) { 
    if (edit.getText().toString().equals("")) { 
     new AlertDialog.Builder(this).setTitle("Ut Oh!") 
       .setMessage("Please enter your current weight.") 
       .setNeutralButton("Try again", null).show(); 
    } 
    else { 
     Intent i = new Intent(getApplicationContext(),GoalWeight.class); 
     i.putExtra("name", name); 
     i.putExtra("current", edit.getText().toString()); 
     startActivity(i); 
    } 
} 

}

package com.example.smallchangebigloss; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

公共类GoalWeight延伸活动{

private EditText edit; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_goal_weight); 
    Bundle extras = getIntent().getExtras(); 
    System.out.println(extras.containsKey("name")); 
    extras.containsKey("current"); 



    edit = (EditText) findViewById(R.id.currentWeightEdit); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.goal_weight, menu); 
    return true; 
} 

public void next(View view) { 
    if (edit.getText().toString().equals("")) { 
     new AlertDialog.Builder(this).setTitle("Ut Oh!") 
       .setMessage("Please enter your Goal Weight.") 
       .setNeutralButton("Try again", null).show(); 
    } 
    else { 
     Intent i = new Intent(getApplicationContext(), Complete.class); 
     i.putExtra("goal", edit.getText().toString()); 
     startActivity(i); 

    } 
} 

}

package com.example.smallchangebigloss; 

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

public class Complete extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle extras = getIntent().getExtras(); 

//   TextView current = (TextView) findViewById(R.id.completeCurrentMod); 
//   current.setText("hi"); 
////   current.setText(extras.getString("current")); These lines break it 

     setContentView(R.layout.activity_complete); 
     System.out.println("name: " + extras.getString("name") + "Current weight: " 
       + extras.getString("current") + "goal: " + extras.getString("goal"));//Only displaying last ones...bundle them everytime in each class? 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.complete, menu); 
     return true; 
    } 

} 
+0

哪些类型的这些数据的? –

回答

1

你所面对的问题是,你需要从一个类传递你的价值观其他每个类的一个很好的例子。因此,在GoalWeight活动中,您还需要传递“名称”和“当前”值。

于是开始做下一个活动时以下。

Intent i = new Intent(getApplicationContext(), Complete.class); 
     i.putExtra("goal", edit.getText().toString()); 
     i.putExtra("name", extras.getString("name")); 
     i.putExtra("current", extras.getString("current")); 
     startActivity(i); 
1

这大致就是你的代码是这样做的:

活动1:

Start with no extras 
Create and save extra called "name" 

活动2:

Read name out of extras 
create and save extra called "current" 
save the value of name 

活动3:

Read the whole bundle of extras 
Create and save extra called "goal" 
//Do nothing with the whole bundle 

活动4:

Read the whole bundle of extras 

内活动3你拉值超出临时演员,但没有“再节约“他们让他们传递到最后的活动。所以当你开始活动4时,你应该拥有的唯一额外部分是"goal",因为这是你在Activity3结尾添加的唯一一个。

+0

那么,我的思维方式是否正确,唯一的方法呢? 将名称添加到演员 - >移到下一个活动。 拉的名字在活动,加上当前和名演员 - >下一个活动...... 等? – Programatt

+0

@Programatt是的,你必须明确地将每个以前的值添加到你用来开始下一个活动的新意图中。 – FoamyGuy

2

看起来您并未将全部extras附加到每个新的Intent。由于您在每个Activity中创建了一个新的Intent,因为您需要这样做,所以还需要附加之前Activities中的所有extras。最简单的方法是创建一个Bundle,然后在每个活动中获取此Bundle,并且每次将新的extras添加到Bundle

另一种方法,你可以做到这一点,所以你不必跟踪extrasBundle在每个是使用SharedPreferences。只需将该值添加到新的首选项中,然后在最终的Activity中获得全部值。这种联系具有创造和获取SharedPreferences

0

在第一个活动,得到了串入一个变量从EditText。而

Intent in = new Intent(getApplicationContext(), B.class); 
in.putExtra("TAG", String); 
startActivity(in); 

在第二个活动,通过

Intent x = getIntent(); 
String variable = x.getStringExtra("TAG"); 

等等得到它..

1

您正在创建每个文本输入活动的新意图,并开始你完成活动。 第一次创建完整活动实例时,您将获得额外的Intent,并根据哪个Activity触发该活动的意图。

如果你完全activity被销毁并重新创建它将具有的意图只能从创建它的一个。

现在,如果已经创建并运行你完成的活动,你是射击与其他活动的意图,你应该看看

http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

@Override 
    public void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     setIntent(intent); 
     // get your new intent data here 
    } 

顺便说一句,在现实世界的应用程序,你不会做你的输入字段分散在各种活动中,所以我假设你只是在尝试一些东西,这很酷。