簡體   English   中英

如何傳遞ArrayList <HashMap<String, String> &gt;從一項活動到另一項活動

[英]How to pass ArrayList<HashMap<String, String>>from one activity to another

我如何將Array List從一個Activity傳遞到另一個我的數組列表如下所示

ArrayList<HashMap<String, String>>

使用putExtra(String, Serializable)傳遞Intent和getSerializableExtra(String)方法中的值以檢索數據。

ArrayList<HashMap<String, String>>從活動A傳遞給活動B.

Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("sunil", "sahoo");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivityForResult(intent, 500);

檢索活動B中的數據

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);

您可以使用Bundle將元素從一個Activity傳遞到另一個Activity。

看看這個: http//developer.android.com/reference/android/os/Bundle.html

您創建Bundle,將其放入Intent,然后在新活動上,獲取它並提取所需的元素。

它是這樣的:

Bundle b = new Bundle();
String s = "hello";
b.putString("example", s);
intent.putExtras(b);

然后是新活動:

Bundle b = this.getIntent().getExtras(); 
String s = b.getString("example");

這是另一種技術,我使用以下行來在firstClass中定義ArrayList。

static ArrayList al=new ArrayList();

在第二個活動中,我使用以下行從firstClass獲取ArrayList的數據,

firstClass.al.size();

我的想法是,你可以為包上的這個數據集定義一個全局靜態變量,並在跳轉到另一個活動之前先保存它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM