将ArrayList <double>传递给另一个类

时间:2018-08-28 12:09:28

标签: java android android-intent arraylist

我可以使用我的代码读取文本文件并将其保存到Arraylist。现在,我想在其他活动中使用此Arraylist,但是在将Arraylist传递给另一个类时遇到了问题。我故意尝试了一下,但没有成功。我将在下面添加程序的代码。

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

希望你能帮助我。

我的第一个活动:

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);

}
}

我的接收者活动:

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();
}
}

3 个答案:

答案 0 :(得分:1)

您需要在接收方活动中调用getIntent()。

Intent intent = getIntent();

然后您可以致电

intent.getExtras()

答案 1 :(得分:1)

Android应用程序的入口点通常是onCreate方法。

我怀疑您的主要方法(传统的Java入口点)未在运行。您可以尝试从onCreate调用它,但应该对其重命名。

要接收Intent,只要您调用主设备并添加Intent代码,就应该发送该Intent,请确保接收方覆盖onReceive。

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

答案 2 :(得分:0)

要在接收方活动中接收双数组列表,可以使用:

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();
}