简体   繁体   中英

Pass hashtable with bundle in Java

I have a hashtable this is my code

class Message
{
    public String message, sender;
    public Message (String msg, String from)
    {
        message = msg;
        sender = from;
    }
    public String toString () { return sender+": "+message; }
}

I am defining my table with this code

Hashtable<String, List<Message>> table = new Hashtable<String, List<Message>>();

How can I pass this table variable with bundle ? Example I am passing string with this code

    Bundle bundle = new Bundle();
    bundle.putString("name", "test");

Now I want to pass table.get("test") this with bundle.

How can I do this ?

Have you tried Bundle#putSerializable() ?

It should work if you add implements Serializable to your Message class, as Hashtable etc. are serializable.

Of course, for getting your Hashtable back from the bundle you'll have to cast it.

Example

Hashtable<String, List<Message>> originalTable = new Hashtable<String, List<Message>>();
// ... put some data into originalTable

// put originalTable into bundle    
Bundle bundle = new Bundle();
bundle.putSerializable("table", originalTable);

// extract table from bundle
Hashtable<String, List<Message>> extractedTable = (Hashtable<String, List<Message>>)bundle.getSerializable("table");

// Now, extractedTable should contain same data as originalTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM