Java Android passing data Array double - android

I have a question about intent in android application
i have an array in class1
double [][] tableCityPair = new double[100][100];
---here code to fill array---
in the end of class i want to send tableCityPair to another class, class2.
how i should declare for array in class2 ?
is this right?
Intent it = getIntent();
double tabelJarakAntarKota= it.getDoubleExtra("tableCityPair",3);

The Bundle class has methods for passing and retrieving an array of doubles:
void putDoubleArray(String, double[])
double [] getDoubleArray(String)
However, these work for one-dimensional arrays. You are attempting to pass a 2D array. I do not know of a direct way to do this. One way to achieve this would be to put N arrays of doubles in a loop.
for(int i=0;i<tableCityPair.length;i++){
bundle.put("tableCity"+i, tableCityPair[i]);
}
And at the receiving end, you do:
double [] aPair = it.getExtras().getDoubleArray("tableCity"+i);
I'm not sure about the performance implications of this though; since you would be adding 100 extras as part of the bundle.
There could be a better way (perhaps make your pair a List<List<Double>> and then implement Parcelable) but I haven't tried any of it so I wouldn't suggest it.

double [][] tableCityPair = new double[100][100];
and then...
make intent it=....
then
put: `it.putExtra("size",tableCityPair.length)`
then:
for(i=0;i<tableCitypair.length;i++)
{
it.putExtra("table"+i,tableVityPair[i][]);
}
THEN in CLASS B
double [][] tableCityPairinB = new double[100][100];
for(i=0;i<getIntent().getExtras("size");i++)
{
double[i][]=getIntent().getExtras("table"+i);
}

Related

Pass List<Double> value from one activity to other [duplicate]

This question already has answers here:
Pass ArrayList<Double> from one activity to another activity on android
(4 answers)
Closed 9 years ago.
I have a list like this,
List< Double> list_r = new ArrayList<Double>();
How can I pass this list from one activity to other ?
How can I pass this list from one activity to other ?
Then Make this list Static just like this:
public static List< Double> list_r = new ArrayList<Double>();
And Access this list in other activity like this:
private List<Double> list_my = ClassName.list_r;
Where ClassName is your Activity which consists (List< Double> list_r).
But make sure I am just showing a way of passing list. But by making List static It will consume memory even after you have finish the use of that arrayList.
You could use a double[] together with putExtra(String, double[]) and getDoubleArrayExtra(String).
Or you can use an ArrayList<Double> together with putExtra(String, Serializable) and getSerializableExtra(String) (the ArrayList part is important as it is Serializable, but the List interface is not).
You may use of bundle how to use: creating bundle and sending over to new activity or save arrayList in shared preferences: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
intent.putExtra("list",list_r);
now on the other activity in onCreate:
getIntent().getParcelableArrayListExtra("list");
use intent.putExtra();
intent.putExtra("array_list", list_r );
Convert double to string, and put it in ArrayList.
In the Activity providing the data, use Intent#putExtra():
double double1 = 0.05;
double double2 = 0.02;
ArrayList <String []> list = new ArrayList <String[]>();
list.add (new String [] {String.valueOf(double_1),String.valueOf(double_2)});
Intent i = new Intent (this,YourTargetActivity.class);
i.putExtra("list", list);
startActivity(i);
Then to retrieve, use Intent#getSerializableExtra() and cast to ArrayList :
ArrayList <String[]> list = (ArrayList<String[]>) getIntent().getSerializableExtra("list");
double double1 = Double.parseDouble(list.get(0)[0]) ;
double double1 = Double.parseDouble(list.get(0)[1]) ;

Ormlite RawRowMapper truncated double

I am using RawRowMapper to load relevant columns from ormlite. RawRowMapper returns all data as String.
The observation is that it truncates the double value.
Example:
Data inserted -> 57.1117146374
Data type used to store the data -> Double
Data from Ormlite when directly queried: -> 57.1117146374 (This is correct and essentially means that ormlite is actually storing the data correctly)
Data from Ormlite when using mapper -> 57.1117 (Truncated data coming as part of String[] resultColumns
Any idea how do I avoid it getting truncated?
EDIT:
#DatabaseField(columnName = "LAT")
private Double lat;
Object Field:
private double lat;
The key here is that the string in resultcolumns[], I get is already truncated.
Data from Ormlite when using mapper -> 57.1117 (Truncated data coming as part of String[] resultColumns
The problem seems to be that getting a double value out as a String is truncated by Android's cursor.getString(...) method. Not sure why but if the result is extracted by using cursor.getDouble(columnIndex); on the same column-index, the full precision is preserved.
The solution here I believe is to map the rows differently. If you use dao.queryRaw(String, DataType[], ...) method, the double field seems to be extracted appropriately. Here's a sample from my test class.
GenericRawResults<Object[]> results =
dao.queryRaw(dao.queryBuilder().selectColumns("lat")
.prepareStatementString(), new DataType[] { DataType.DOUBLE });
CloseableIterator<Object[]> iterator = results.closeableIterator();
try {
assertTrue(iterator.hasNext());
Object[] objs = iterator.next();
assertEquals(foo.doubleField, objs[0]);
} finally {
iterator.close();
}
You could also use a custom row mapper and the dao.queryRaw(String, RawRowMapper, ...) method to convert and return a custom object with a double field.

How to pass a 2D array of double between activities?

I need to pass a 2D array of double between activities,this is what i wrote so far :
In the first activity (the sender ) :
Intent intent =new Intent(MapsActivity.this, RoutePath.class);
Bundle b = new Bundle();
b.putSerializable("other_locations", other_locations);
intent.putExtras(b);
startActivity(intent);
In the second one (the receiver ):
Intent intent = getIntent();
Bundle b = intent.getExtras();
double[][] other_locations=(double[][]) b.getSerializable("other_locations");
But I got the ClassCastException, am I doing something wrong?
Convert double 2D array to String 2D array and send it just like you did :
To send :
b.putSerializable("array", other_locations_string);
To get :
String[][] value = (String[][]) b.getSerializable("array");
And then again convert it into double 2d array.
The reason for this behaviour is because Java (and consequently Android) does not let you cast Objects to primitive types, or arrays of primitive types. Why? Because Java considers a valid cast one which converts a super-class object to a sub-class object or vice versa. String[][] extends Object, while double and double[][] do not.
Try this
Convert 2D Array into Hashmap and then use it
Map<Double, Double> map = new HashMap<Double, Double>(other_locations.length);
for (Double[] mapping : other_locations)
{
map.put(mapping[0], mapping[1]);
}
And set map as intent extra and get the extra in second activity

Anyway to have more than one extra added to intent?

I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent

How to pass ArrayList<Coordinate[]> from one Activity to another

I have tried all day to do this and haven't had any luck. I have an ArrayList with an Array of Coordinate types inside, and I want to pass this from one activity to another. I don't think this is possible through using Intent. So I am wondering what other option do I have? I want something simple..
I'd probably put the values in a database in your first activity and only pass some unique identifier on to the second activity so it can recreate the ArrayList on the other side. But if you want a quick solution, use the intent bundle and serialize/deserialize the coordinates yourself:
ArrayList<Coordinate> mCoords = new ArrayList<Coordinate>();
private void startOtherActivity()
{
int numCoords = mCoords.size();
Intent intent = new Intent();
intent.putExtra("coord_size", numCoords);
for (int i = 0; i < numCoords; i++)
{
Coordinate coord = mCoords.get(i);
intent.putExtra("coord_x_" + i, coord.getX());
intent.putExtra("coord_y_" + i, coord.getY());
}
//start other activity...
}
Then grab the values out of the bundle on the other side and regen the ArrayList.
Edit: Just realized you're talking about Coordinate[]s. Same principle still applies, just nest another for loop in there and keep track of each individual array's length as well.
Update: On the other side, you'd have something like
int len = intent.getIntegerExtra("coord_size", 0);
for(int i = 0; i < len; i++)
{
float x = intent.getFloatExtra("coord_x_" + i, 0.0);
float y = intent.getFloatExtra("coord_y_" + i, 0.0);
mCoords.add(new Coordinate(x, y));
}
You can have a singleton "Model" class, on which you can store the ArrayList object from one activity, and retrieve it from another.
Create Bundle and try to put the ArrayList. Then send the bundle in the intent.
You could write a ParcelableCoordinate class that implements Parcelable, and then have it convert the Coordinate objects into ParcelableCoordinate objects, put them in the intent, and then do the reverse on the other side.

Categories

Resources