Android - How to pass HashMap String,String between activities?
This is pretty simple, All Collections
objects implement Serializable
(sp?) interface
which means they can be passed as Extras inside Intent
Use putExtra(String key, Serializable obj)
to insert the HashMap
and on the other Activity
use getIntent().getSerializableExtra(String key)
, You will need to Cast the return value as a HashMap
though.
Passing Hashmap through android intents
Sending HashMap
HashMap<String, Object> hashmapobject =new HashMap<>();
HashMap<String, Object> newhashMap=new HashMap<>();
hashmapobject.put("key",newhashMap);
Intent intent = new Intent(SenderActivity.this, NextActivity.class);
intent.putExtra("hashMapKey", hashmapobject);
startActivity(intent);
Receiving HashMap
Intent intent = getIntent();
HashMap<String, Object> hashMapObject = (HashMap<String, Object>) intent.getSerializableExtra("hashMapKey");
HashMap<String, Object> newhashMap=(HashMap<String, Object>)hashMapObject.get("key");
passing a HashMap from one activity to another, android
Use intent.putExtra(String, Serializable) - see http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29.
i.e.
intent.putExtra("placesHashMap", places)
In the receiving activity use
HashMap<String, String> places = (HashMap<String, String>) intent.getSerializableExtra("placesHashMap");
Android:Passing a hash map between Activities
I am assuming you own both Activities (call them A and B). In which case just put the map in a public static variable and access it from B via A.data_map.
[update]
For all of the downvotes take a peek at the Android Application Framework FAQ section "How do I pass data between Activities/Services within a single application?". The solution I recommend is exactly the same as...
A public static field/method
An alternate way to make data
accessible across Activities/Services
is to use public static fields and/or
methods. You can access these static
fields from any other class in your
application. To share an object, the
activity which creates your object
sets a static field to point to this
object and any other activity that
wants to use this object just accesses
this static field.
Yes, there are caveats to this solution but with the limited info presented by the OP we can not assume this method will not work.
pass hashmap data from an activity
Use intent.putExtra(String, Serializable) - see
intent.putExtra("hashMap", fileList);
In the receiving activity use
HashMap<Integer, boolean[]> fileList = (Integer, boolean[]) intent.getSerializableExtra("hashMap");
And also chnage the below code
Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);
To
Intent intent = new Intent(context, MainActivity.class);
How to pass HashMap String,List String from intent
You are doing it wrong, all Collections are serializable, in order to pass hashmap into intent you use
putExtra(String key, Serializable obj)
and to get the array back from intent, you do
getIntent().getSerializableExtra(String key)
Your Answer
btnsave = (Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent( getApplicationContext(), S.class );
intent.putExtra( "details", getData() );
startActivity(intent);
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show();
}
}
});
public HashMap< String, List< String > > getData() {
HashMap< String, List< String > > expandableListDetail = new HashMap<>();
for ( int i = 2 ; i < container.getChildCount() ; i++ ) {
if ( container.getChildAt( i ) instanceof RelativeLayout ) {
List< String > childs = new ArrayList<>();
childs.add( ( ( TextView ) container.getChildAt( i ).findViewById( R.id.textout ) ).getText().toString() );
expandableListDetail.put( txtHeading.getText().toString(), childs );
}
}
return expandableListDetail;
}
Android - How to pass HashMap String,List String between activities?
Try below code:
Intent intent=new Intent(CURRENT CLASS.this, CLASS TO CALL.class);
intent.putExtra("test", YOUR HASHMAP);
startActivity(intent);
In another Activity get data like below:
HashMap<String, List<String>> data=(HashMap<String, List<String>>) this.getIntent().getSerializableExtra("test");
How to send hashmap value to another activity using an intent
Java's HashMap class extends the Serializable
interface, which makes it easy to add it to an intent, using the Intent.putExtra(String, Serializable)
method.
In the activity/service/broadcast receiver that receives the intent, you then callIntent.getSerializableExtra(String)
with the name that you used with putExtra.
For example, when sending the intent:
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);
And then in the receiving Activity:
protected void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
Log.v("HashMapTest", hashMap.get("key"));
}
Related Topics
Android App Crashes When Launched in Debug Mode
Webview Link Click Open Default Browser
Removing Unused Strings During Proguard Optimisation
Only Load Layout When Firebase Calls Are Complete
Android Pendingintent Extras, Not Received by Broadcastreceiver
Simulate Low Network Connectivity for Android
Using Resultreceiver in Android
How to Use Onsaveinstancestate() and Onrestoreinstancestate()
How to Open/Display Documents(.Pdf, .Doc) Without External App
How to Scale Bitmap to Screen Size
Find Out If Listview Is Scrolled to the Bottom
Failed Binder Transaction When Putting an Bitmap Dynamically in a Widget
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
Full Screen Dialogfragment in Android