2014-06-11 3 views
0
String address = info.getAddress(); 

      if(address != null && !address.isEmpty()) 
      { 
       TextView txtSearch = (TextView) getView().findViewById(R.id.text_search); 
       txtSearch.setText(address);} 

안녕하세요, 위의 모든 활동은 내가 주소를 가져오고 텍스트를 설정하기 위해 쿼리하는 클래스입니다. 어떻게하면 내 다른 활동을 textView로 설정할 수 있습니까? 활동 1에서 얻은 주소 결과가 수업에 포함됩니까? 미리 감사드립니다. activity2에에서 onCreate에서어떻게 다른 활동에서 얻은 결과로 텍스트를 설정할 수 있습니까?

// Create an intent to launch the second activity 
    Intent intent = new Intent(getBaseContext(), activity2.class); 

    // Pass the text from the edit text to the second activity 
    intent.putExtra("address", address); 

    // Start the second activity 
    startActivity(intent); 

: activity1에에서

+0

http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android –

답변

0

// Get the intent that was used to launch this activity 
    Intent intent = getIntent(); 

    // Get the text that was passed from the main activity 
    String address= intent.getStringExtra("address"); 
0

이 있습니다 당신이 귀하의 요구 사항에 따라 선택할 수있는 방법을 다릅니다. 편도은 인 텐트를 다른 대답에서 제안하고 있습니다. 첫 번째 활동에서 (여기서 의도를 사용하여 두 번째 활동을 시작할 수 있습니다 당신은 의도와 함께 주소를 보내고있다.)

Intent i=new Intent(context,ACTIVITY.class); 
    i.putExtra("add", ADDRESS); 
    context.startActivity(i); 

두 번째 활동,

Intent intent = getIntent(); 
String address= intent.getStringExtra("add"); 

을 당신이 필요로하지 않는 경우 Intent를 사용하려면 첫 번째 활동 내에서 데이터를 SharedPreferences에 저장하고 두 번째 활동에서 데이터를 검색 할 수 있습니다. 저장하려면 데이터

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE); 
shared.edit().putString("add", "ADDRESS").commit(); 

데이터

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE); 
     String add=shared.getString("add", null); 

를 얻으려면 아니면 당신은 캐시에 저장하고 두 번째 활동에서 얻을 수 있습니다.

0

두 액티비티간에 문자열 주소 만 공유하는 경우 가장 간단한 방법은 다른 답변에 설명 된대로 인 텐트와 함께 putExtra를 사용하여 추가 데이터로 보낼 수 있습니다.

그러나 여러 액티비티에서 주소를 사용하고 모든 액티비티에 대해 동일한 주소를 사용해야하는 경우 (하나의 액티비티가 주소를 변경하면 모두 변경됨) 고려해야합니다. SharedPreferences를 사용합니다.

String address = info.getAddress(); 
String prefName = "address"; 
SharedPreferences prefs; 
prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
prefs.edit().putString(prefName, address).commit(); 

그리고 어떤 활동에서 데이터를 검색 :

SharedPreferences shared = getSharedPreferences(prefName, MODE_PRIVATE); 
String address = shared.getString(prefName, null); 

당신이 할 수 있도록, "주소"는 이름의 공유 현가없는 경우 해결하기 위해 할당 될 것입니다 '널' pref가 이미 존재하는지 확인하기 위해 해당 값을 테스트하십시오.

관련 문제