1. 程式人生 > >ArrayList<HashMap<String,Object>>集錦

ArrayList<HashMap<String,Object>>集錦

ble ++ ret size 一個 num use pri sdn

1. Android中如何從一個Activity中ArrayList<HashMap<String,Object>>傳遞到另一個activity?

eg:

存:intent.putExtra("arrayList", dataList);
取(記得強制類型轉換):
ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
dataList = (ArrayList<HashMap<String, Object>>

) getIntent().getSerializableExtra("arrayList");

2. ArrayList<HashMap<String, Object>>如何更新鍵值對和刪除鍵值對?

eg:

String[ ] name= new String[ ]{"張三","李四","王五","趙六","霍七"};

ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
for( int i=0 ; i <name.length ; i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("Name", name[ i ]); arrayList.add(map); } // 修改制定key for (HashMap<String, Object> hashMap : arrayList)
{ if (hashMap.get("Name").equals("王五")) { hashMap.put("Name", "小明"); } } // 官方推薦的刪除方法 Iterator<HashMap<String, Object>> iterator = arrayList.iterator(); while(iterator.hasNext()) { if(iterator.next().get("Name").equals("李四")) { iterator.remove(); } } System.out.println(arrayList); 運行結果: [{Name=張三}, {Name=小明}, {Name=趙六}, {Name=霍七}] 3. ArrayList<HashMap<String, Object>>循環獲取數據 eg: 添加數據: Data = new ArrayList<Map<String, Object>>();

Map<String,Object> map1 = new HashMap<String,Object>();

map1.put("username",et_username.getText().toString().trim());

map1.put("password",et_password.getText().toString().trim());

Data.add(map1);

取值: for(Map<String, Object> m : Data){

if(m!=null){

String username = m.get("username").toString().trim();

String password = m.get("password").toString().trim();

et_username.setText(username);

et_password.setText(password);

}else{

return;

}

}

ArrayList<HashMap<String,Object>>集錦