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]) ;
Related
I want to pass Array list from one activity to another activity. I am trying like this:
I am passing array list source activity to destination activity. But the problem is I am getting only the last item at destination activity.
My code is
source.class
HashMap<String,String> hm = new HashMap<String, String>();
ArrayList<HashMap<String,String>> arl = new ArrayList<HashMap<String,String>>();
hm.put(KEY_NAME,u);//am adding these values through loop
arl.add(hm);//adding Hash Map to Array List
Intent intent = new Intent(MainActivity.this, SinglePlaceActivity.class);
intent.putExtra("arraylist", arl
startActivityForResult(intent, 500);
System.out.println("uuuuu"+arl);//upto now working good and display perfectly all array list
destination.class
ArrayList<HashMap<String, String>> arl = ArrayList<Hash
Map<String,String>>)getIntent().getSerializableExtra("arraylist");
System.out.println(arl);//am getting what i add last item in the Arrylist at source class
Iterator itr = arl.iterator();
while(itr.hasNext())
{
System.out.println(itr.hasNext);//am getting single last item multiple times.what i add last item in the Arrylist at source class
I want to display source class array list into destination class.
Try using:
Bundle.putSerializable() and Bundle.getSerializable().
Also, in:
System.out.println(itr.hasNext);
shouldn't it be its.next()?
Take also into account that using Seralizable object in Android IPC adds a significant overheard with respect to using Parcelable objects.
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
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);
}
i am developing a final year project where i need to retrieve details of hospitals like name ,address, location(in terms of latitude and longitude) from a server and display them on a map....connectivity has been established and i am able to retrieve the values in the from of array like address[], name[] etc..
now i need to pass these values from an activity class to map activity class...i am new to intents..can anyone please provide codes or relevant links which may be helpful in solving this problem
any help will be really appreciated....cheers :)
You can add data to an Intent using one of the putExtra routines. But serializing large amounts of data is expensive. Take a look here for alternatives.
I Suggest store that all necessary data in static list and then use that static .
public static List<String> name = new ArrayList<String>();
public static List<String> address = new ArrayList<String>();
public static List<String> latitude = new ArrayList<String>();
public static List<String> longitude = new ArrayList<String>();
name.add("your data");
address .add("your data");
latitude .add("your data");
longitude .add("your data");
then you can get all the data in any activity .
I have
ArrayList<String> ids = ArrayList<String>();
What would be the cleanest way to make it Parceleable? Apparently String itself is not parcelable, so Parcel.writeList(ids) is not working.
I was thinking to either parcelize ArrayList<Uri> or put array contents into a Bundle.
Convert your list to String[] and use Parcel.writeStringArray. Example here
This isnt parcelizing, but to put it in a bundle:
ArrayList<String> ids = new ArrayList<String>();
savedInstanceState.putStringArrayList("key",ids);