参数的数量取决于之前的活动

时间:2013-05-25 09:01:06

标签: java android

我正在尝试获取之前活动提供的参数。参数的数量不固定,我的方法应该能够读取所有参数(从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();
}

非常感谢!!

1 个答案:

答案 0 :(得分:2)

您可以使用Bundle.putDoubleArray()Bundle.getDoubleArray()无论如何都会更简单。