我想将一个int和intent.putExtra传递给另一个活动。我在主类的菜单中的对话框内。我的问题是我怎么做,因为我不知道在哪里或如何把它。这是我的代码:
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.numero_ejercicios_settings){
dialogSpinner();
Intent i = getIntent();
i.putExtra(Constantes.NUMERO_TOTAL, numero_total);
return true;
}
return super.onOptionsItemSelected(item);
}
public void dialogSpinner(){
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Example");
String[] types = {"5", "10"};
b.setItems(types, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which){
case 0:
numero_total = 10;
break;
case 1:
numero_total = 10;
break;
}
}
});
b.show();
}
答案 0 :(得分:1)
你可以这样试试:
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.numero_ejercicios_settings){
dialogSpinner();
return true;
}
return super.onOptionsItemSelected(item);
}
public void dialogSpinner(){
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Example");
String[] types = {"5", "10"};
b.setItems(types, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0:
numero_total = 10;
break;
case 1:
numero_total = 10;
break;
}
dialog.dismiss();
Intent intent = new Intent(this, NameOfActivityToStart.class);
intent.putExtra(Constantes.NUMERO_TOTAL, numero_total);
startActivity(intent);
}
});
b.show();
}
注意我使用了new Intent()
。如果您使用getIntent()
,我基本上会再次启动相同的活动。
答案 1 :(得分:-1)
定义最终
final Intent intent = new Intent()
然后你可以把它放到下面。但你不能第二次初始化