Passing an ArrayList of LatLng through intent - java

Right now, I know how to send an ArrayList of Strings through intent by doing this to send:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putStringArrayListExtra("list", list);
startActivity(intent);
and this to receive:
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("list");
But I want to send an ArrayList of LatLngs, like this:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putLatLngArrayListExtra("listLatLng", listLatLng);
startActivity(intent);
\\pseudo code, obviously
and then:
Intent i = getIntent();
ArrayList<String> list = i.getLatLngArrayListExtra("listLatLng");
\\pseudo code, obviously
Any ideas on how to do this? Is there any easy way or do I have to modify something? Thanks.

I would use android.location; It extends Parcelable and you can easily create and store the Locations in an arrayList created from lat and lon values
Intent i = new Intent(MainActivity.this, MapActivity.class);
i.putParcelableArrayListExtra(name, value);

Related

Null Object Reference on Intent with Arraylist<Hashmap>

I've got a Problem passing my Arraylist to the next Activity.
Here is my Error Code.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
By my understanding, i am trying to get an Arraylist, which does not exist.
I made sure the Arraylist is filled and my Intend values are correct, but im still getting the Error.
Creating the Arraylist
ArrayList<HashMap<String, String>> names = new ArrayList<HashMap<String, String>>();
Creating the intent
Intent intent = new Intent(First.this, Second.class);
intent.putExtra("names", names);
startActivity(intent);
Getting the intent
Intent intent = getIntent();
ArrayList<HashMap<String, String>> usernamen = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("names");
As said, the Arraylist get's filled correctly via
name = new HashMap<String, String>();
name.put("Name", spielername.getText().toString());
name.put("Gender", gender.getText().toString());
names.add(name);
If someone needs more information, i'll be happy to provide them.
Edit:
gobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(names.size() < 2){
Toast.makeText(Spieler.this, "No Informations", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Spieler.this, Games.class);
intent.putExtra("level", level.getProgress());
intent.putExtra("names", names);
startActivity(intent);
}
});
The Button is sending me to the second Activity, so the Array has to be filled.
ArrayList<HashMap<String,String>> names = new ArrayList<>();
HashMap<String,String> map = new HashMap<>();
map.put("Name", "Spieler");
map.put("Gender", "male");
names.add(map);
Send the data with Intent
Bundle bundle = new Bundle();
bundle.putSerializable("keyName",names);
Intent i=new Intent(v.getContext(), SecondActivity.class);
i.putExtras(bundle);
startActivity(i);
Get the data from Intent
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
Log.e("SecondActivity","yourArrayList--"+(ArrayList<HashMap<String,String>>)bundle.getSerializable("keyName"));
You need to serialize the value first, with either using Bundle:
Bundle bundle = new Bundle();
bundle.putSerializable("names", namen);
intent.putExtras(bundle);
or casting the list with Serializable:
intent.putExtra("names", (Serializable) names);

I want to send integer,String,Object,double,Bitmap,HashMap from one activity to another in android

I am begnning in android , I want to send String,Object,double,ArrayList from one activity to another . I already done with integer but when i want to send other data like object and ArrayList i am facing problem.
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Passing double data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found
Passing String data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");
or
Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");
Passing ArrayList data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");
Passing Object data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");
Note : Keep in mind your custom Class must implement the Serializable interface.
Passing HashMap data :
SenderActivity
HashMap<String, String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");
Passing Bitmap data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");
You can send object one activity to another by using Serializable class
check following example:
http://hmkcode.com/android-passing-java-object-another-activity/
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
you can send like:
Intent intent = new Intent(mcontext, activity.class);
intent .putExtra("Key",product);
startActivity(intent );
and get value like:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();`enter code here`
product = (ClassName) bundle.getSerializable("Key");
You can make a bean of the data you want to send like this
public class YourBean implements Serializable {
String yourString;
Object yourObject;
double yourDouble;
ArrayList<String> yourList;
public String getYourString() {
return yourString;
}
public void setYourString(String yourString) {
this.yourString = yourString;
}
public Object getYourObject() {
return yourObject;
}
public void setYourObject(Object yourObject) {
this.yourObject = yourObject;
}
public double getYourDouble() {
return yourDouble;
}
public void setYourDouble(double yourDouble) {
this.yourDouble = yourDouble;
}
public ArrayList<String> getYourList() {
return yourList;
}
public void setYourList(ArrayList<String> yourList) {
this.yourList = yourList;
}
}
then when you want to pass data in intent do like this
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
YourBean yourBean = new YourBean();
yourBean.setYourString("your string");
yourBean.setYourDouble(Your double);
yourBean.setYourObject(Your Object);
yourBean.setYourList(array list);
intent.putExtra("bean",yourBean);
startActivity(intent);
then you can get it like this in your SecondActivity like this
YourBean yourBean1 = (YourBean) getIntent().getSerializableExtra("bean");
You can use intents. In a intent you can put all sort of data, String, int, whatever you want.
In your case, in activity1, before going to activity2, you will store a String message this way :
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);
In activity2, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Then you can set the text in the TextView or where you want
Hope this helps you !
Intent mIntent = new Intent(MainActivity.this, AboutActivity.class);
ArrayList<String> mArray = new ArrayList<>();
mArray.add("Mon");
mArray.add("Tue");
mIntent.putStringArrayListExtra("Array", mArray);
startActivity(mIntent);
// In AboutActivity.java recieved data
ArrayList<String> mArray= getIntent().getExtras().getStringArrayList("Array");
Toast.makeText(AboutActivity.this, ""+mArray, Toast.LENGTH_LONG).show();

What is the difference between getIntent() and new Intent() in Android?

I do like this
first way
Intent intent = new Intent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
second way
Intent intent = getIntent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
Both can give the same result.I want to know the actual difference between this two
In the context of an Activity, getIntent() will return the Intent that was originally sent to the Activity. The example you gave may work the same, but you should really avoid using getIntent() if you are passing the Intent to another Activity or sending it back as a result.
For example:
If I start an activity with:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("key", "test");
startActivity(intent);
Then in my MainActivity class:
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
So now consider if you have SecondActivity and I am starting it from MainActivity using getInent();
Intent intent = getIntent();
intent.setClassName("com.example.pkg", "com.example.pkg.SecondActivity"");
intent.setComponent(new ComponentName("com.example.pkg", "com.example.pkg.SecondActivity"));
intent.putExtra("isLoggedIn",true);
startActivity(intent);
Then in my SecondActivity I can access key and isLoggedIn both.
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
boolean testIsLoggedIn = intent.getBooleanExtra("isLoggedIn",true);
So, generally it is not good practice to use the getIntent to start further activities.

Passing data between 2 activities

I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");

Action before startActivity()

I'm new in Android development and I've a problem when I create my new activity.
I want to use my activity before start it. For example, I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,MyActivity.class);
Screen.this.startActivity(intent);
But, my "menu" and "MyActivity.class" aren't the same instance. Consequently I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,menu);
Screen.this.startActivity(intent);
But it doesn't work...
Have you a solution for help me?
Thanks for help and sorry for the bad english.
You can't do that like you want, if you are looking to pass data between an activity you must use Extras, and you can pass just Serializable items.
First Context (can be Activity/Service etc)
You have a few options:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) Use the putExtra() shortcut method of the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
New Context (can be Activity/Service etc)
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(key);
}
NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.
you don't have to create the new activity yourself, Android system does it for you.
If you want to go from Screen Activity to MyActivity, you can do this :
Intent intent = new Intent(Screen.this,MyActivity.class);
startActivity(intent);
and then, in you MyClass java file, in the onCreate method, you could do :
this.setXmppreception(reception);
This way, I think you get you wanted, no?

Categories