2016-09-26 115 views
0

我在這裏有一個代碼,我想通過單擊保存按鈕來傳遞哈希映射函數。我看了Here,但找不到解決方案。如何從意圖傳遞HashMap <String,List <String>>

上點擊按鈕,我需要通過hasmap.Anyone請幫助this.My代碼是:

btnsave = (Button) findViewById(R.id.btn_save); 
    btnsave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      try { 
       expandableDataPump.getData(); 
       Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
//HERE I NEED TO PASS HASHMAP 
       intent.putStringArrayListExtra("details",(HashMap<String,>) expandableDataPump.getData()); 

       startActivity(intent); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 

       Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
      } 


     } 
    }); 

} 

public class ExpandableDataPump { 
    public HashMap<String, List<String>> getData() { 
     HashMap<String, List<String>> expandableListDetail = new HashMap<>(); 

     for (int i = 2; i < container.getChildCount(); i++) { 
      if (container.getChildAt(i) instanceof RelativeLayout) { 
       List<String> childs = new ArrayList<>(); 
       childs.add(((TextView)container.getChildAt(i).findViewById(R.id.textout)).getText().toString()); 
       expandableListDetail.put(txtHeading.getText().toString(), childs); 
      } 
     } 


     return expandableListDetail; 
    } 
} 

}

+0

你有什麼問題?你得到任何異常/崩潰? –

+0

雖然編寫代碼的意圖紅線出現,我不能運行它.. –

回答

2

你做錯了,一切都Collections序列化,爲了通過HashMap的進入意圖使用

putExtra(String key, Serializable obj) 

,並從意圖獲取數組回來,你做

getIntent().getSerializableExtra(String key) 

你的答案

btnsave = (Button) findViewById(R.id.btn_save); 
btnsave.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     try { 
      Intent intent = new Intent(getApplicationContext(), S.class); 
      intent.putExtra("details", getData()); 

      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
     } 


    } 
}); 

public HashMap< String, List<String> > getData() { 
    HashMap< String, List<String> > expandableListDetail = new HashMap<>(); 

    for (int i = 2 ; i < container.getChildCount() ; i++) { 
     if (container.getChildAt(i) instanceof RelativeLayout) { 
      List<String> childs = new ArrayList<>(); 
      childs.add(((TextView) container.getChildAt(i).findViewById(R.id.textout)).getText().toString()); 
      expandableListDetail.put(txtHeading.getText().toString(), childs); 
     } 
    } 
    return expandableListDetail; 
} 
+0

你可以編輯上面的代碼 –

+0

以及它如何與哈希映射..我很困惑.. –

+0

intent.putStringArrayListExtra(「details」,( HashMap )expandableDataPump.getData()); 我在這裏做錯了嗎? –

0

使用

extras.putSerializable("HashMap",Hash_Map); 
intent.putExtras(extras); 

而不是

intent.putStringArrayListExtra( 「詳細信息」,(HashMap的)expandableDataPump.getData());

btnsave = (Button) findViewById(R.id.btn_save); 
btnsave.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     try { 
      expandableDataPump.getData(); 
      Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
      intent.putExtra("details",(HashMap<String,>) expandableDataPump.getData()); 

      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 

      Toast.makeText(MainActivity.this, "You have an ERROR", Toast.LENGTH_LONG).show(); 
     } 


    } 
}); 

}

+0

演員和Hash_map顯示錯誤? –

+0

使用我提到的下面的代碼..不需要添加額外的。 Intent intent = new Intent(getApplicationContext(),ExpandableList.class); intent.putExtra(「details」,(HashMap )expandableDataPump.getData()); startActivity(intent); –

+0

intent.putExtra(「details」,(HashMap )expandableDataPump.getData());這句話是不完整的 –

2

您可以通過以下只是下面的指令做到這一點。

首先使用實現Serializable創建類,如下所示。

public class DataWrapper implements Serializable{ 

    private HashMap<String,List<String>> hasmap; 

    public DataWrapper(HashMap<String,List<String>> hasmap){ 
     this.hasmap= hasmap; 
    } 

    public HashMap<String,List<String>> getHashMap(){ 
     return this.hasmap; 
    } 
} 

從你想要通過的活動HashMap<String,List<String>>把下面的代碼。

DataWrapper dw = new DataWrapper(expandableDataPump.getData()); 

Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
intent.putExtra("details", dw); 
startActivity(intent); 

在你ExpandableList活動取得DataWrapper對象,如下圖所示,從該對象檢索您HashMap

try{ 
    Intent intent = getIntent(); 
    DataWrapper dw = (DataWrapper) intent.getSerializableExtra("details"); 
    HashMap<String,List<String>> hasmap = dw.getHashMap(); 
} catch(Exception e){ 
} 

就是這樣。它爲我工作。

+0

如果代碼與我的上述代碼有關 –

+0

@jitesh爲什麼要在hashmap上創建另一個包裝呢?直到和除非用戶有一個自定義類 –

+0

@lephaphase我更改代碼請參閱根據您的代碼。 –

0

要通過HashMap

btnsave.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     try { 
      Intent intent = new Intent(getApplicationContext(), ExpandableList.class); 
      intent.putExtra("details", expandableDataPump.getData()); 
      startActivity(intent); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 
}); 

ExpandableList檢索,

HashMap<String,List<String>> hashmap = (HashMap<String,List<String>>) getIntent().getSerializableExtra("details");