0

I'm trying to pass an HashMap object to another activity using getter tableRow.getRowData() as shown in code below:

tableRow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent newIntent = new Intent(getApplicationContext(), ToDoDetailActivity.class);
                putExtras(newIntent, tableRow.getRowData());//<---
                startActivity(newIntent);
            }
        });

Actually tableRow object belong to a custom class that inherits from TableRow which inside an HashMap<String, String> rowData that represents the entire row (key: 'DB column', value: 'DB field value'). As I said, rowData is returned by getRowData().

Is this a good way to transfer object within putExtras()?

Also, seeing this Android tutorial, I can't figure how to deal with rowData to the other activity.

EDIT: My question is a bit different from this because it's not possible to pass, even if HashMap class is serialized by default, hash map object through putExtra() (I've tryed to do so by previously seen that question).

Community
  • 1
  • 1
Andrea Grimandi
  • 631
  • 2
  • 8
  • 32
  • I think there is no issue by passing data with this way. But there are many other ways also. – RajSharma Aug 21 '15 at 08:23
  • 4
    possible duplicate of [Android - How to pass HashMap between activities?](http://stackoverflow.com/questions/4992097/android-how-to-pass-hashmapstring-string-between-activities) – Mihir Shah Aug 21 '15 at 08:25

4 Answers4

1

AFAIK, there's no way to pass HashMap through Activity. If you really need to pass all HashMap data, I recommend to use Application to store your HashMap then you can access your HashMap data from everywhere in Application.

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • It's probably one solution but I don't think that's a good idea provide that as global variable. I'll try another way calling much `putExtra("key", "value")` many time as the number of HashMap's record. – Andrea Grimandi Aug 21 '15 at 08:55
  • @AndreaGrimandi yes, it's not a good solution (but not bad of course). However, you should consider it if you need pass a lot of data to another activity. If your data is big when passing, It will make your app force stop. – Kingfisher Phuoc Aug 21 '15 at 09:11
0

Yes, that's the standard way, but you only can pass Serializable objects as extras. HashMap and String implement Serializable, so it should work.

It migth be useful to have a look to that TableRow inheriting class, but I'll try to guess some code to send:

Intent newIntent = new Intent(getApplicationContext(), ToDoDetailActivity.class);
intent.putExtra("tableRow", tableRow.getRowData());//<---
startActivity(newIntent);

And to get TableRow data from within the receiving Activity:

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    TableRow tableRow = (TableRow)intent.getSerializableExtra("tableRow");
}
Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
0

you don't need to use getRowData. all that you must do is :

HashMap<Object, Object> extraData = new HashMap<>();
putExtras("myHashMap", extraData);

and in your second activity:

(HashMap<Object, Object>) intent.getSerializableExtra(YourFirstActivity.EXTRA_DATA);
Fakher
  • 2,098
  • 3
  • 29
  • 45
0

I've figured out that, as kind Kingfisher Phuoc said, ther's no way to pass HashMap through Activities, unless calling putExtra() for each element of the HashMap. I think that's the simplest solution ever.

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32