简体   繁体   中英

How to display username in a String contained in an array

I can't figure out how to display the username from the previous activity in the second activity.

I have tried a bunch of method but none of them get me to display the Username in the String

This is the MainActivity :

       private EditText nameField;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nameField = findViewById(R.id.nameEditText);
    Button startButton = findViewById(R.id.startButton);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = nameField.getText().toString();
            getusername(name);
        }
    });

}

private void getusername(String name) {
    Intent intent = new Intent(this, OneShot.class);
    Resources resources = getResources();
    String key = resources.getString(R.string.key_name);
    intent.putExtra(key, name);
    startActivity(intent);
}

This is the SecondActivity :

 public static final String TAG = OneShot.class.getSimpleName();
private String name;
private int nextSentenceId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_one_shot);

    Intent intent = getIntent();
    name = intent.getStringExtra(getString(R.string.key_name));
    if (name == null || name.isEmpty()) {
        name = "Player";
    }
    Log.d(TAG, name);

    final int[] sentences = new int[]{R.string.page0, R.string.page2, R.string.page3, R.string.page4, R.string.page5 };

    Button next= findViewById(R.id.next);
    final TextView displayText= findViewById(R.id.displayText);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int limit = 5;
            if(nextSentenceId < sentences.length){
                displayText.setText(sentences[nextSentenceId]);
                ++nextSentenceId;
            }
        }
    });

}

Those are the Strings in the int[] :

     <string name="page0"> Hello %1$s </string>
<string name="page2"> Welcome into this application </string>
<string name="page3"> How are you ? </string>
<string name="page4"> How is the weather </string>
<string name="page5"> Is %1$s you\'re real name ? </string>

Why isn't the username passed in the string sentences and how can I fix it ? Thanks in advance.

Change:

  1. strings in your array to lead them to correct format:

     <string name="page0">Hello %s</string> <string name="page5">Is %s your real name?</string> 
  2. call to set the text in the TextView in accordance with the format above:

     displayText.setText(String.format(Locale.getDefault(), getString(sencences[nextSentenceId]), name)); 

What you have in the int array is not the String values, but rather the ids that are used to link to said String resource. You can get the String resource(value) associated with the id using getResources().getString(R.string.yourstring) .

//So, instead of 
//displayText.setText(sentences[nextSentenceId]);

//use
displayText.setText(getResources().getString(sentences[nextSentenceId]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM