How to pass ArrayListHashMapString, String from one Activity to a Fragment?
Try this to send your list
ArrayList<HashMap<String, String>> mapArrayList= new ArrayList<>();
MyFragment mFrag = new MyFragment();
Bundle args = new Bundle();
args.putSerializable("LIST", (Serializable) mapArrayList);
mFrag.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(container_id, mFrag).commit();
and get your list inside your fragment like this
Bundle myBundle=getArguments();
if(myBundle!=null){
ArrayList<HashMap<String, String>> mapArrayList= myBundle.getSerializable("LIST");
}
How to pass ArrayListHashMapString, Stringfrom one activity to another
Use putExtra(String, Serializable)
to pass the value in an Intent and getSerializableExtra(String)
method to retrieve the data.
Passing an ArrayList<HashMap<String, String>>
from Activity A to Activity B
Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("sunil", "sahoo");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivityForResult(intent, 500);
Retrieve the data in Activity B
ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
How do I pass an ArrayListHashMapString, String between Activities?
ArrayList are Serializable (as well as HashMaps and Strings), so try putExtra(String, Serializable)
.
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("key", list);
pass ArrayListHashMapString, String to a fragment
You can create listener callback interfaces and implement them in your fragments. Something like this:
@Override
public void onSomeEvent(List<SomeData> data) {
//do something with data
}
In your activity create this interface:
public interface OnSomeEventListener {
onSomeEvent(List<SomeData> data);
}
then obtain your fragment by using findFragmentById or findFragmentByTag and assign it to a listener:
this.onSomeEventListener = fragment;
You can then call methods of that interface and your fragment will receive callbacks.
The second and more easier way of communication between fragments and activities is BroadcastReceivers. You can register some BroadcastReceiver in your fragments and then call sendBroadcast() from activity. Your list of data can be put in a bundle of that broadcast message.
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"));
}
pass the arraylist with hashmap between activities in android?
Use putExtra(String, Serializable)
to pass the value in an Intent and getSerializableExtra(String)
method to retrieve the data.
ArrayList implements Serializable
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
You already have this
ordernow.putExtra("orderlist",childgame);
To get the list
Intent intent = getIntent();
ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("orderList");
public Serializable getSerializableExtra (String name)
Added in API level 1
Retrieve extended data from the intent.
Parameters
name The name of the desired item.
Returns
the value of an item that previously added with putExtra() or null if no Serializable value was found.
Related Topics
Setting Android Theme Background Color
Sqlite Simultaneous Reading and Writing
Page Scroll When Soft Keyboard Popped Up
Android: How to Create Fade-In/Fade-Out Sound Effects for Any Music File That My App Plays
Retrofit2: Modifying Request Body in Okhttp Interceptor
Newer Versions of Android Studio and Only Two Drawable Directory - Drawable and Drawable-V21
Tablayout Tab Title Text in Lower Case
Fragmentactivity Cannot Be Resolved to a Type
Android Button Background Color
Uploading Image from Android to Gcs
5.1 Crash - a Taskdescription's Primary Color Should Be Opaque
How to Set an Imageview's Image from a String
Hax Kernel Module Is Not Installed
My Android Camera Uri Is Returning a Null Value, But the Samsung Fix Is in Place, Help