2012-01-04 58 views
1

我有數組列表中的散列。如何將哈希表的Arraylist轉換爲其他意圖?

List<Hashtable<String, String>> info = new ArrayList<Hashtable<String, String>>(); 


Hashtable<String, String> hm = new Hashtable<String, String>(); 
// Put elements to the map 

hm.put("Read_Flag", s1); 
hm.put("sms_received_id", s2); 
hm.put("Sender_Id", s3); 
hm.put("Sender_Name", s4); 
hm.put("Patient_Name", s5); 
hm.put("Received_Date", s6); 
hm.put("Received_Text", s7); 
hm.put("Received_Text_Full", s8); 
hm.put("AttachmentFlag", s9); 

// Get a set of the entries 
Set<?> set = hm.entrySet(); 
// Get an iterator 
Iterator<?> it = set.iterator(); 
// Display elements 
while(it.hasNext()) { 
Map.Entry me = (Map.Entry)it.next(); 
// System.out.print(me.getKey() + ": "); 
// System.out.println(me.getValue()); 
} 
//System.out.println(hm); 

info.add(hm); 

此處的信息包含我的哈希表。我怎樣才能得到這個「信息」對象到其他類/意圖?

謝謝你......

+0

這可能只是我,但我不明白你問什麼。也許一個例子或更多的解釋會有所幫助。 – Thomas 2012-01-04 11:04:36

+0

不確定你的意思?你能否簡化一下? – 2012-01-04 11:04:55

+0

我怎樣才能得到這個「信息」對象在其他類? – Anand 2012-01-04 11:06:13

回答

6

創建擴展SerializablegettersetterList<Hashtable<String, String>> list

Custom.java

public class Custom implements Serializable{ 

    private static final long serialVersionUID = 4466821913603037341L; 
    private List<Hashtable<String, String>> list; 


    public List<Hashtable<String, String>> getList() { 
     return list; 
    } 

    public void setList(List<Hashtable<String, String>> list) { 
     this.list = list; 
    } 
} 

要傳遞到下一個活動的類。

List<Hashtable<String, String>> list = new ArrayList<Hashtable<String,String>>(); 

Custom custom = new Custom(); 
custom.setList(list); 
intent.putExtra("myobj", custom); 

要檢索下一個活動

Intent intent = getIntent(); 
Custom custom = (Custom) intent.getSerializableExtra("myobj"); 
List<Hashtable<String, String>> list = custom.getList(); 
+0

謝謝你......它適用於我.. – Anand 2012-01-04 12:07:44

0

將您的哈希表來捆綁,並通過使用intent.putExtra()的捆綁

+0

給我一些示例代碼 – Anand 2012-01-04 11:08:06

2
List<Hashtable<String, String>> info = new ArrayList<Hashtable<String, String>>(); 
String[] selItemArray = new String[info .size()]; 
      //copy your List of Hashtable Strings into the Array ,and then pass it in your intent 
      // .... 
      Intent intent = new Intent(InfoClass.this, AnotherClass.class); 
      intent.putExtra("info_array", selItemArray); 
      startActivityForResult(intent, 0); 

類型的數組列表

putIntegerArrayListExtra(String name, ArrayList<Integer> value) 

     putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) 

     putStringArrayListExtra(String name, ArrayList<String> value) 

     putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value) 

    Then you can read from you next activity by replacing put with get with key string as argument,eg 

myIntent.getStringArrayListExtra("key"); 

陣列:

Bundle b=new Bundle(); 
b.putStringArray(key, new String[]{value1, value2}); 
Intent i=new Intent(context, Class); 
i.putExtras(b); 



In order to read: 

Bundle b=this.getIntent().getExtras(); 
String[] array=b.getStringArray(key); 

希望這會對你有所幫助。

參見:Passing a list of objects between Activities

+0

我怎樣才能得到這個在其他類? – Anand 2012-01-04 11:12:31

+0

我編輯答案 – jennifer 2012-01-04 11:19:54

+0

它聽起來不錯,但我不想陣列,我想Arraylist有散列表。怎麼做? – Anand 2012-01-04 11:27:23

1

將您的哈希表來捆綁,並通過使用intent.putExtra() 這裏的例子

意向書I =新意圖(這一點,yourclass.class)的捆綁;

i.putExtra(「info」,info); 。

,你可以在其他意圖

捆綁額外= getIntent()getExtras得到的值();

extra.geStrinf(「info」);

相關問題