roa.tah
roa.tah

Reputation: 567

passing a HashMap from one activity to another, android

I am writing an android application to display nearby locations. I stored then in a hashmap list like this

List<HashMap<String, String>> places = null;

in the first activity I displayed places on a map, and I want to pass them to another activity to list then in a listView.

in the first activity I have a button to direct me to the list activity like this:

public void list_airports(View v)
        {
            Intent intent;
            switch (v.getId()) {

            case R.id.list_items:
                intent = new Intent(getApplicationContext(), List_airports.class);
                intent.putExtra("places",places);
                startActivity(intent);
                break;

            }
            }

in the next activity I did this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_airports);
        Bundle extras = getIntent().getExtras();
        String[] places=extras.getStringArray(Intent.EXTRA_TEXT);

but the method putExtra doesn't accept List<HashMap<String, String>>

Upvotes: 3

Views: 5648

Answers (3)

QuokMoon
QuokMoon

Reputation: 4425

What is Serializable

Serializable take screenshot of your current class

1. Passes intent from context class to new class

HashMap<String, String> hashMap= adapter.getItem(position);
Intent intent = new Intent(OneActivity.this, SecondActivity.class);
intent.putExtra("key", value);
startActivity(intent);

2.Retrive hash map to other class

Bundle b=getIntent.getExtra();
 if(b!=null)
   {
     yourhasmaplist=b.getSerializableExtra("key"); 
   }

Upvotes: 0

Binita Mehta
Binita Mehta

Reputation: 339

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");

Upvotes: 6

Joel Fernandes
Joel Fernandes

Reputation: 4856

Use putExtra(String, Serializable) to pass it in Intent and getSerializableExtra(String) method to retrieve it.

Also, use ArrayList instead of List

Upvotes: 1

Related Questions