Nice programing

인 텐트를 사용하여 해시 맵 값을 다른 활동에 보내는 방법

nicepro 2020. 11. 22. 20:34
반응형

인 텐트를 사용하여 해시 맵 값을 다른 활동에 보내는 방법


HashMap하나의 의도에서 두 번째 의도 을 보내는 방법은 무엇입니까?

또한 HashMap두 번째 활동에서 해당 을 검색하는 방법은 무엇입니까?


Java의 HashMap 클래스는 Serializable인터페이스를 확장 하므로 Intent.putExtra(String, Serializable)메소드를 사용하여 인 텐트에 쉽게 추가 할 수 있습니다.

인 텐트를 수신하는 활동 / 서비스 / 브로드 캐스트 수신기 Intent.getSerializableExtra(String)에서 putExtra에서 사용한 이름으로 호출 합니다.

예를 들어 인 텐트를 보낼 때 :

HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);

그리고 수신 활동에서 :

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
    Log.v("HashMapTest", hashMap.get("key"));
}

이것도 효과가 있기를 바랍니다.

보내는 활동에서

 Intent intent = new Intent(Banks.this, Cards.class);
                            intent.putExtra("selectedBanksAndAllCards", (Serializable) selectedBanksAndAllCards);
                            startActivityForResult(intent, 50000);

수신 활동에서

    Intent intent = getIntent();
    HashMap<String, ArrayList<String>> hashMap = (HashMap<String, ArrayList<String>>) intent.getSerializableExtra("selectedBanksAndAllCards");

다음과 같이 HashMap을 보낼 때

Map<String,ArrayList<String>> selectedBanksAndAllCards = new HashMap<>();

누군가에게 도움이되기를 바랍니다.

참고 URL : https://stackoverflow.com/questions/7578236/how-to-send-hashmap-value-to-another-activity-using-an-intent

반응형