简体   繁体   English

传递 ArrayList<Double> 到另一个班级

[英]Passing ArrayList<Double> to another class

I can read and save a textfile to an Arraylist with my code.我可以使用我的代码读取文本文件并将其保存到Arraylist Now I want to use this Arraylist in other activities but I have problems with passing the Arraylist to another class.现在我想在其他活动中使用这个Arraylist ,但是我在将Arraylist传递给另一个类时遇到了问题。 I tried it with intent but it didnt work.我有意地尝试过,但没有奏效。 I will add the codes of my program below.我将在下面添加我的程序代码。

Intent intent=new Intent(this, ergebnisse.class);
intent.putExtra("NEFZ", Nlist);
startActivity(intent);

I hope you can help me.我希望你能帮助我。

My first activity:我的第一个活动:

public class NEFZ2array extends AppCompatActivity implements Serializable {

public static void main(String[] args) {
    FileReader file = null;
    try {
        file = new FileReader("NEFZ.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ArrayList<Double> Nlist = new ArrayList<Double>();
    int i=0;
    Double d= null;
    try {
        BufferedReader input = new BufferedReader(file);
        String s=null;
        while((s=input.readLine())!=null) {
            StringTokenizer st = new StringTokenizer(s,",");
            while(st.hasMoreTokens()) {
                try {
                    d = Double.parseDouble(st.nextToken());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Nlist.add(i, d);
            }
        }
        input.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
    for(double j:Nlist) {
        System.out.println(j);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nefz2array);

}
}

My Receiver Activity:我的接收器活动:

public class ergebnisse extends AppCompatActivity implements Serializable {

public Button button_ausfuerlicher;
public Button button_home;

public void init(){
    button_ausfuerlicher = (Button) findViewById(R.id.button_ausfuerlicher);
    button_ausfuerlicher.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(ergebnisse.this, ausfuehrlicher.class);

            startActivity(intent);
        }
    });
}

public void init2(){
    button_home = (Button) findViewById(R.id.button_home);
    button_home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(ergebnisse.this, MainActivity.class);

            startActivity(intent);
        }
    });
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ergebnisse);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    init();
    init2();
}
}

You need to call getIntent() in your receiver activity. 您需要在接收方活动中调用getIntent()。

Intent intent = getIntent();

and then you can call 然后你可以打电话

intent.getExtras()

The entry point to an android application is typically the onCreate method. android应用程序的入口点通常是onCreate方法。

I suspect that your main method (traditional java entry point) is not being run. 我怀疑您的主要方法(传统的Java入口点)未在运行。 You could try calling it from the onCreate, but should really rename it. 您可以尝试从onCreate调用它,但实际上应该重命名它。

To receive an Intent, which should be sent as long as you call your main and add the Intent code, make sure the receiver overrides onReceive. 要接收一个Intent,只要您调用主设备并添加Intent代码,就应该发送该Intent,请确保接收方重写onReceive。

@Override
public void onReceive(Context context, Intent intent) {
    ArrayList<Double> NEFZ = intent.getExtras().getString("NEFZ");
}

To receive the double array list in the receiver activity you can use: 要在接收器活动中接收双数组列表,可以使用:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ergebnisse);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

        // Get double list from sender activity
        List<Double> doubleList = (ArrayList<Double>) getIntent().getSerializableExtra("NEFZ");

        // TODO: Process your double list here

        init();
        init2();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM