我正在尝试获取先前 Activity 提供的参数。参数的数量不固定,我的方法应该能够读取所有参数(从 1 到 n)。参数的数量由参数n_inputs 给出。我尝试使用以下代码来执行此操作,编译器是正确的,但它有一些问题,我不知道在哪里......我认为它一定与数组 param[ ] 有关。你能帮我一下吗?
double params[];
int n_inputs;
@Override
protected void onCreate(Bundle savedInstanceState)
{
//I'm gonna get the parameters from the previous activity
Bundle b = getIntent().getExtras();
//here I get the number of inputs from the previous activity
n_inputs = b.getInt("ninputs");
// I create a new array with dimension "i_inputs"
params= new double[n_inputs];
if(n_inputs>0)
{
for (int i=0;i<n_inputs;i++)
{
params[i] = b.getDouble("param"+(i+1));
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Now I try to show the parameters in a toast
Context context = getApplicationContext();
CharSequence text = "parameter 1 "+params[0] +" param 2 "+ params[1] ;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
非常感谢!!
最佳答案
您可以使用Bundle.putDoubleArray()和 Bundle.getDoubleArray()无论如何都会更简单。
关于java - 参数的数量取决于之前的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16747996/