반응형
인 텐트를 사용하여 해시 맵 값을 다른 활동에 보내는 방법
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<>();
누군가에게 도움이되기를 바랍니다.
반응형
'Nice programing' 카테고리의 다른 글
파이썬에서 변수 이름을 어떻게 인쇄 할 수 있습니까? (0) | 2020.11.22 |
---|---|
응답에서 텍스트 읽기 (0) | 2020.11.22 |
두 지리적 위치 사이의 거리 계산 (0) | 2020.11.22 |
Xcode-구성 : 오류 : $ PATH에 허용 가능한 C 컴파일러가 없습니다. (0) | 2020.11.22 |
선택 쿼리의 MySQL IF ELSEIF (0) | 2020.11.22 |