Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

android - 如何使用putExtra()和getExtra()来表示字符串数据(How to use putExtra() and getExtra() for string data)

Can someone please tell me how exactly to use getExtra() and putExtra() for intents?

(有人可以告诉我如何使用getExtra()putExtra()进行意图?)

Actually I have a string variable, say str, which stores some string data.

(实际上我有一个字符串变量,比如str,它存储一些字符串数据。)

Now, I want to send this data from one activity to another activity.

(现在,我想将这些数据从一个活动发送到另一个活动。)

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );

and then in the SecondScreen.java

(然后在SecondScreen.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.

(那就是我有一个editText,我得到的字符串无论用户类型如何。)

Then with the help of myEditText.getText().toString() .

(然后在myEditText.getText().toString()的帮助下。)

I am getting the entered value as a string then I have to pass this data.

(我将输入的值作为字符串,然后我必须传递此数据。)

  ask by Shaista Naaz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...