我在soap消息中工作過,爲了解析來自Webservice的值,值存儲在ArrayList中。如何在android中使用字典(如iphone的NSDictionary)?
示例: 值是Employee name(Siva)和Employee id(3433fd),這兩個值都存儲在arraylist中,但我想存儲在Dictionary中,怎麼樣?
我在soap消息中工作過,爲了解析來自Webservice的值,值存儲在ArrayList中。如何在android中使用字典(如iphone的NSDictionary)?
示例: 值是Employee name(Siva)和Employee id(3433fd),這兩個值都存儲在arraylist中,但我想存儲在Dictionary中,怎麼樣?
您可以使用HashMap中這樣
Map <String,String> map = new HashMap<String,String>();
//add items
map.put("3433fd","Siva");
//get items
String employeeName =(String) map.get("3433fd");
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);
}
}
一個Dictionary是一個抽象類,鍵映射到值並沒有字典類android系統是指link你可以在類概述發現一張紙條
不要使用這個類,因爲它已經過時了。請使用地圖 界面進行新的實施。
,則嘗試插入使用一個null鍵或空值的字典將導致一個NullPointerException,但你可以使用Map接口,它提供了一個dictionary.you完全相同的功能,可以使用它作爲波紋管
//put values
Map Message = new HashMap();
Message.put("title", "Test message");
Message.put("description", "message description");
Message.put("email", "[email protected]");
//get values
Log.d("get Message","Message title -"+Message.get("title"));
你也可以使用自定義類,如下
public class MessageObject {
String title;
String description;
String email;
}
你可以使用getter和setter獲取和放置值,不需要記住鍵名前夕您可以通過這種方式獲得其他優勢。
感謝您的回覆。 – 2012-01-14 09:04:38
是你在找什麼? – confucius 2012-01-14 09:05:10
這些也是像(iPhone詞典) – 2012-01-14 09:07:45