0

I have a problem with passing an ArrayList to another activity in Android Studio.

I created a ArrayList that holds the data type Double. The program should read it and print it in the console.

This is the code from the first activity:

 Intent intent = new Intent(this, ergebnisse.class);
 intent.putExtra("WLTP_list", Wlist);
 startActivity(intent); 

And this is the code from the second activity:

Bundle bundle = getIntent().getExtras();
ArrayList<Double> Wlist = (ArrayList<Double>) bundle.getDoubleArray("WLTP_list");

The part after the last equal sign is marked red and the error is:

Inconvertible types; cannot cast 'double[]' to 'Java.util.ArrayList<Java.lang.Double>' 

I didn't change the type so I don't know why it is giving me that error.

Jacob
  • 299
  • 1
  • 18
  • Please show a [mcve]. What does `bundle.getDoubleArray` returns? Or, what `Wlist` is in the first snippet? – user202729 Jul 30 '18 at 15:46
  • Is because `getDoubleArray` return `double[]` not `ArrayList`.See this doc https://developer.android.com/reference/android/os/BaseBundle – Crammeur Jul 30 '18 at 15:47
  • 2
    Possible duplicate of [Pass ArrayList from one activity to another activity on android](https://stackoverflow.com/questions/11648312/pass-arraylistdouble-from-one-activity-to-another-activity-on-android) – Belbahar Raouf Jul 30 '18 at 15:54

4 Answers4

1

You are calling bundle.getDoubleArray(), which returns an array, instead of an ArrayList, that's why you're getting the error as you're trying to convert an double[] array into an ArrayList.

Dom
  • 1,427
  • 1
  • 12
  • 16
1

The getDoubleArray() method returns an array of type double[], which cannot be explicitly cast to ArrayList. If you really want to use it as an ArrayList, you need to get that double[] array and explicitly convert it to an ArrayList. For instance, something like

 ArrayList<Double> Wlist = ArrayList<Double>(Arrays.asList( bundle.getDoubleArray("WLTP_list")));
Ricardo Costeira
  • 3,171
  • 2
  • 23
  • 23
1

Trying this way.

First Activity:

 Intent intent = new Intent(this, SecondActivity.class);
    ArrayList<Double> listDouble = new ArrayList<Double>();
    listDouble.add(1.0);
    listDouble.add(2.0);
    intent.putExtra("WLTP_list", listDouble);
    startActivity(intent);

Second Activity:

    ArrayList<Double> listDouble = (ArrayList<Double>) getIntent().getSerializableExtra("WLTP_list");
System.out.println("Return.." + listDouble);
sommesh
  • 923
  • 9
  • 19
0

Try this code in your second activity instead:

ArrayList<Double> Wlist = (ArrayList<Double>) getIntent().getSerializableExtra("WLTP_list");

The current method you are using returns an Array of the data type Double, not an ArrayList. The code I provided should give you an ArrayList instead.

If that line doesn't work, you can always look at this answer.

Jacob
  • 299
  • 1
  • 18