-------修订-------------------------------------
在本课程中,我进行了计算:
...
ArrayList<Double> final_cores =new ArrayList<Double>();
...
public void cores_func(){
double initcores=Double.parseDouble(num_cores.getText().toString().trim());
double half_time=Double.parseDouble(halftimecores.getText().toString().trim());
double ttime=Double.parseDouble(timecores.getText().toString().trim());
double l=Math.log(2)/half_time;
double fcores=initcores*Math.exp(-l*ttime);
for (int i=0;i<=ttime;i++){
final_cores.add(initcores*Math.exp(-l*ttime));
}
//convert ArrayList to double[]
double[] mydata = new double[final_cores.size()];
for(int i = 0; i < final_cores.size(); i++) {
mydata[i] = final_cores.get(i);
}
Intent i=new Intent(this,core_calcs.class);
i.putExtra("value",fcores);
i.putExtra("value2",initcores);
i.putExtra("value3",ttime);
i.putExtra("final_cores",mydata);
startActivity(i);
}
...
并在linegraph类中:
...
private double [] final_cores =new double [100];
public double [] getFinal_cores(){ return this.mydata;}
public void setFinal_cores( double [] final_cores){ this.mydata=mydata;}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
...
double [] mydata = extras.getDoubleArray("final_cores");
setFinal_cores(mydata);
public Intent getIntent(Context context){
...
double []mydata=getFinal_cores();
ArrayList<Double> x =new ArrayList<Double>();
ArrayList<Double> y =new ArrayList<Double>();
// ArrayList<Double> data =new ArrayList<Double>();
//fill x,y values
for (int i=0;i<=10;i++){
x.add(ttime / 10.0 * i);
}
for (int i=0;i<=10;i++){
y.add(initcores + ((mydata[i]) / 10.0 * i));
}
TimeSeries series = new TimeSeries("Decay");
for (int i=0;i<x.size();i++){
series.add(x.get(i),y.get(i));
}
答案 0 :(得分:2)
1)不幸的是,似乎没有一种简单的方法来传递双ArrayLists。有两种方法可以解决这个问题:
a)你需要创建一个包装double的类(注意:基元而不是类)并扩展Parcelable(指南在这里:http://developer.android.com/reference/android/os/Parcelable.html)。如果此类名为NewDouble,则可以使用以下代码:
ArrayList<NewDouble> final_cores = (ArrayList<NewDouble>)extras.getParcelableArrayList("final_cores");
b)如果在将ArrayList放入bundle之前将其转换为double [],则可以将getDoubleArray()用于double数组。
将ArrayList转换为double数组的快速代码:
double[] doubleArray = new double[final_cores.size()];
for(int i = 0; i < final_cores.size(); i++) {
doubleArray[i] = final_cores.get(i);
}
答案 1 :(得分:2)
因此,要将ArrayList<Double>
从一个活动传递到另一个活动,您必须使用Parcelable
界面。
您必须创建一个名为MyDouble的类,例如:
public class MyDouble implements Parcelable {
Double al;
public MyDouble(Parcel source){
al = source.readDouble();
}
public MyDouble(Double val){
al = val;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return this.hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeDouble(al);
}
public Double getValueDouble(){
return this.al;
}
public static Parcelable.Creator<MyDouble> CREATOR = new Parcelable.Creator<MyDouble>() {
@Override
public MyDouble createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new MyDouble(source);
}
@Override
public MyDouble[] newArray(int size) {
// TODO Auto-generated method stub
return new MyDouble[size];
}
};
}
实例化MyDouble的ArrayList并使用:
Intent second = new Intent(this, Second.class);
ArrayList<MyDouble>al = new ArrayList<MyDouble>();
MyDouble m1 = new MyDouble(2.0);
MyDouble m2 = new MyDouble(4.0);
al.add(m1);
al.add(m2);
second.putParcelableArrayListExtra("array_list_test", al);
startActivity(second);
您可以通过执行以下操作检索其他活动中的双打值:
ArrayList<MyDouble> p =b.getParcelableArrayList("array_list_test");
我在我的机器上进行了测试,它就是这样的。希望它有所帮助。
一些文档:
http://developer.android.com/reference/android/os/Parcelable.html
http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html
http://blog.cluepusher.dk/2009/10/28/writing-parcelable-classes-for-android/