34

I have worked in soap message, and to parse the value from Webservice, the values are stored in ArrayList.

Example: values are Employee name (Siva) and Employee id (3433fd), these two values are stored in arraylist, but I want to stored in Dictionary, How?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sampath Kumar
  • 1,650
  • 4
  • 14
  • 16

4 Answers4

82

you can use HashMap like this

Map <String,String> map =  new HashMap<String,String>();
//add items 
map.put("3433fd","Siva");
//get items 

String employeeName =(String) map.get("3433fd");
confucius
  • 13,127
  • 10
  • 47
  • 66
11

You can use Bundle.

as it offers String to various types of Mapping.

Bundle b = new Bundle();
b.putInt("identifier", 121);
b.putString("identifier", "Any String");
b.putStringArray("identifier", stringArray);

int i = b.getInt("identifier");
...
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Thanks. A lot of answers point to hash map but actually I think bundle is more similar to dictionary because it allows more types of variables – Shayno Jul 30 '17 at 14:35
6
 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText textview = (EditText) findViewById(R.id.editTextHash);
        EditText textviewNew = (EditText) findViewById(R.id.editTextHash2);

        Map<String, String> map = new HashMap<String,String>();
        map.put("iOS", "100");
        map.put("Android", "101");
        map.put("Java", "102");
        map.put(".Net", "103");

        String TextString = "";
       // Set<E> keys = map.keySet();

       Set keys = map.keySet();
       System.out.println("keys "+keys);
        for (Iterator i = keys.iterator(); i.hasNext();) 
        {
            String key = (String) i.next();
            System.out.println("key "+key);
            String value = (String) map.get(key);
            System.out.println("value "+value);
            textview.append(key + " = " + value);
            TextString+=(key + " = " + value); 
        }
        //textviewNew.setText(TextString);

//        Iterator iterator = map.keySet().iterator();
//        
//        while (iterator.hasNext()) 
//        {
//          String object = (String) iterator.next();
//          textview.setText(object);
//      }
//        
//        //textview.setText(map.get("Siva"));
//        
//        System.out.println(map);

    }
}
Sampath Kumar
  • 1,650
  • 4
  • 14
  • 16
2

A Dictionary is an abstract class that maps keys to values and there is Dictionary Class in android refer link you can find a note at Class Overview

Do not use this class since it is obsolete. Please use the Map interface for new implementations.

An attempt to insert either a null key or a null value to a dictionary will result to a NullPointerException,but you can use Map interface,it provides the exact same functionality of a dictionary.you can use it as bellow

//put values
Map Message = new HashMap();
Message.put("title", "Test message");
Message.put("description", "message description");
Message.put("email", "email@gmail.com");
//get values
Log.d("get Message","Message title -"+Message.get("title"));

you can also use A custom class as below

public class MessageObject {
    String title;
    String description;
    String email;
}

you can use Getters and Setters to get and put values,not needed to remember the key names every time you may get some other advantages by this way.

Shabeer Ali
  • 903
  • 11
  • 20