我有三项活动:
我必须将var“Name”从登录传递给选择(并且这项工作很好)然后,从选择到输入,这就是我遇到问题的地方:/我可以将名称传递给选择,但是当我尝试通过它进入,我不能!这很奇怪,因为如果我直接从登录到条目传递变量,它可以工作:/ 所以:
这是从登录传递到选择
的代码Intent intent;
String pkg=getPackageName();
intent=new Intent(getApplicationContext(), scelta.class);
//inseriamo i dati nell'intent
String parts[] = risp.split("/");
intent.putExtra(pkg+".myNome", parts[0]);
intent.putExtra(pkg+".myId", parts[1]);
startActivity(intent);
这是选择(可能是错误):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scelta);
// l'intent di questa activity
Intent intent=getIntent();
String pkg=getPackageName();
//prendiamo i dati
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
intent.putExtra(pkg+".myNome", nome);
intent.putExtra(pkg+".myId", Id);
TextView tvNome = (TextView) findViewById(R.id.txtNome);
tvNome.setText(nome);
}
//pulsante per il checkin
public void checkin (View v) {
// l'intent di questa activity
Intent intent=getIntent();
String pkg=getPackageName();
//prendiamo i dati
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
//li reinseriamo nell'intent
intent.putExtra(pkg+".myNome", nome);
intent.putExtra(pkg+".myId", Id);
intent=new Intent(getApplicationContext(), entrata.class);
startActivity(intent);
}
checkin是我点击按钮从选择到进入的方式时使用的方法。这就是我在Entry中取名的地方:
Intent intent=getIntent(); // l'intent di questa activity
String pkg=getPackageName();
String nome=intent.getStringExtra(pkg+".myNome"); //prendiamo i dati
TextView tvNome = (TextView) findViewById(R.id.nome);
tvNome.setText(nome);
感谢大家:)
答案 0 :(得分:5)
您将值放在Intent对象中,然后创建了不同的Intent来启动上一个活动。
public void checkin (View v){
Intent intent=getIntent();
//first intent you created.
String pkg=getPackageName();
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
intent.putExtra(pkg+".myNome", nome);
intent.putExtra(pkg+".myId", Id);
//you are adding extra in first intent
intent=new Intent(getApplicationContext(), entrata.class);
// here you again recreated an Intent object second intent
startActivity(intent);
// you are start activity using second intent which do not contain any extra
}
通过::
更改此内容 public void checkin (View v){
Intent intent=getIntent();
String pkg=getPackageName();
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
Intent intent1 = new Intent(getApplicationContext(), entrata.class);
intent1.putExtra(pkg+".myNome", nome);
intent1.putExtra(pkg+".myId", Id);
startActivity(intent);
}
答案 1 :(得分:2)
您也可以使用SharedPreferences。在Activity中定义SharedPreferences对象。将您想要的数据放在LoginActivity中。从您喜欢的每个Activity中的SharedPreferences对象获取数据:
在登录活动中:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
preferences.edit().putString("MY_NOME",this.nome).commit();
preferences.edit().putString("MY_ID",this.id).commit();
在其他活动中:
SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
String myNome = preferences.getString("MY_NOME", null);
String myId = preferences.getString("MY_ID", null);
答案 2 :(得分:0)
可能与两次要求额外内容有关(onCreate和checkin方法)。您可以尝试仅在onCreate方法内检索附加项,并将值存储在实例变量中(与实例中的所有方法共享)。类似的东西:
public class choice extends Activity {
private String nome;
private String id; //first letter lowercase in variables: Java conventions
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scelta);
// l'intent di questa activity
Intent intent = getIntent();
String pkg = getPackageName();
//prendiamo i dati
this.nome=intent.getStringExtra(pkg+".myNome"); //this is now an instance variable
this.id=intent.getStringExtra(pkg+".myId"); //this is now an instance variable
intent.putExtra(pkg+".myNome", this.nome);
intent.putExtra(pkg+".myId", this.id);
TextView tvNome = (TextView) findViewById(R.id.txtNome);
tvNome.setText(this.nome);
}
//pulsante per il checkin
public void checkin (View v){
// l'intent di questa activity
//Intent intent=getIntent(); //NO NEED FOR THIS ANYMORE
//String pkg=getPackageName();//NO NEED FOR THIS ANYMORE
//prendiamo i dati
//String nome=intent.getStringExtra(pkg+".myNome"); //NO NEED FOR THIS ANYMORE
//String Id=intent.getStringExtra(pkg+".myId"); //NO NEED FOR THIS ANYMORE
//li reinseriamo nell'intent
intent=new Intent(getApplicationContext(), entrata.class);
intent.putExtra(pkg+".myNome", this.nome);
intent.putExtra(pkg+".myId", this.id);
startActivity(intent);
}
}
我认为这是更优化的。如果{on {1}}或nome
自onCreate后未发生变化,则无需再次添加。如果是这样,也许onCreate是使用id