티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to use putExtra() and getExtra() for string data

문자열 데이터에 대해 putExtra() 및 getExtra()를 사용하는 방법

 문제 내용 

Can someone please tell me how exactly to use getExtra() and putExtra() for intents? Actually I have a string variable, say str, which stores some string data. Now, I want to send this data from one activity to another activity.

getExtra()와 putExtra()를 정확하게 사용하는 방법을 알려주실 수 있나요? 사실 저는 문자열 데이터를 저장하는 문자열 변수, 예를 들어 str을 가지고 있다. 이제 한 액티비티에서 다른 액티비티로 이 데이터를 보내고 싶습니다.
  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );

 

and then in the SecondScreen.java

그리고 나서 Second Screen.java에서.
 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        TextView userName = (TextView)findViewById(R.id.userName);
        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("strName")!= null)
        {
            //TODO here get the string stored in the string variable and do 
            // setText() on userName 
        }

    }

 

I know it is very basic question but unfortunately I am stuck here. Please help.

나는 그것이 매우 기본적인 질문이라는 것을 알지만 불행하게도 저는 여기에 막혔습니다. 제발 도와주세요.

 

Thanks,

감사해요.

 

Edit: Here the string which I am trying to pass from one screen to the other is dynamic. That is I have an editText where I am getting string whatever user types. Then with the help of myEditText.getText().toString() . I am getting the entered value as a string then I have to pass this data.

편집: 여기서 내가 한 화면에서 다른 화면으로 전달하려는 문자열은 동적입니다. 즉, 사용자가 무엇을 입력하든 문자열을 받는 편집 텍스트가 있습니다. 그런 다음 EditText.getText().toString()의 도움을 받아 입력된 값을 문자열로 가져오고 이 데이터를 전달해야 합니다.

 

 

 

 높은 점수를 받은 Solution 

Use this to "put" the file...

이를 사용하여 파일을 "넣음"...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

 

Then, to retrieve the value try something like:

그런 다음 값을 검색하려면 다음과 같이 하세요.
String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

 

 

 가장 최근 달린 Solution 

send

보내기
startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));

 

get

가져오기
String myData = getIntent.getStringExtra("key");

 

 

출처 : https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data

반응형
댓글
공지사항
최근에 올라온 글