I have an activity where the values are retrieved from the ResultSet and I store them in an array as:

flat[i] = rs.getDouble(3);
flng[i] = rs.getDouble(4);
i++;
Intent i = new Intent(MapID.this, map.class);
Bundle bundle=new Bundle();
bundle.putDoubleArray("Lat", flat);
bundle.putDoubleArray("Long", flng);
i.putExtras(bundle);
startActivity(i);

On the other class I get the values by:

Bundle bundle = getIntent().getExtras();
flat = bundle.getDoubleArray("Lat");

I want to convert the values to String something like:

String xx = new Double(flat).toString();
有帮助吗?

解决方案 3

Use String.valueOf on each element of your array and store it to another String array like this:

double[] flat = bundle.getDoubleArray("Lat");
String[] flatAsStrings = new String[flat.length];

for (int i = 0; i < flat.length; i++) {
    flatAsStrings[i] = String.valueOf(flat[i]);
}

To simply print these float values without converting it to Strings, you can do it like this:

double[] flat = bundle.getDoubleArray("Lat");
System.out.println(Arrays.toString(flat));

Will print something like (depends on your input):

[1.0, 2.0, 3.0, 4.0, 5.0]

其他提示

In Java, the + operator is defined for primitives. You can simply do something like String xx = "" + myDouble;

Although it is recommended to use the String methods like this : String xx = String.valueOf(myDouble);

I don't know what did you want that convert the values to String. If you want to convert single double number to String, you can use this:

String.valueOf(double);

But I think you need to changed all numbers of the double array to String, you may use this:

Arrays.toString(double[]);

If you need to put the all result's data to bundle, you may use this:

Bundle.putSparseParcelableArray(String key, SparseArray<? extends Parcelable> value);

SparseArray is very useful in android. It likes java.util.Map, but the key must be integer.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top