kamil1995b
kamil1995b

Reputation: 121

getText().toString() not working, returns empty

I'm trying to save userName but the saved text file always returns , 6. How can I get it to show whatever value of userName entered into EditText, and the rest? for example Don, 6. I have read you have to use getText() but that isn't returning anything in the saved file.

However, if I replace 6 with an intent to receive score from previous activity, this works! like this...

        Bundle extras = getIntent().getExtras();
        int score = extras.getInt("Score");

So this becomes...

public void addListenerToEndButton() {

    quit = (Button) findViewById(R.id.endBtn);
    userName = (EditText) findViewById(R.id.userName);

    Bundle extras = getIntent().getExtras();
    int score = extras.getInt("score");

    quit.setOnClickListener(new View.OnClickListener() {

        String strName = userName.getText().toString();

        @Override
        public void onClick(View v) {
            saveProgress(strName + ", " + score, "results.txt");
            finish();
            System.exit(0);
        }
    });
}

But it still returns empty, whatever score is. For example , 4.

I've read this post that suggests it should be inside onClickListener which it is: EditText getText().toString() not working

This is my saveProgress class:

public void saveProgress(String contents, String fileName) {

    try {
        File fp = new File(this.getFilesDir(), fileName);
        FileWriter out = new FileWriter(fp);
        out.append(contents);
        out.append("\n\r");
        out.close();
    }

    catch (IOException e) {
        Log.d("Me","file error:" + e);
    }
}

Upvotes: 0

Views: 628

Answers (2)

GoneUp
GoneUp

Reputation: 385

I guess you understood 'inside onClickListener' wrong. What you are doing atm is that you read strName when you create the listener, but I guess you want to read it when quit is clicked.

So just move the line into the function and the value will be correct.

public void addListenerToEndButton() {

    quit = (Button) findViewById(R.id.endBtn);
    userName = (EditText) findViewById(R.id.userName);

    quit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String strName = userName.getText().toString();
            saveProgress(strName + ", " + 6, "results.txt");
            finish();
            System.exit(0);
        }
    });
}

Upvotes: 0

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

Change your onClick() method with the following:

public void addListenerToEndButton() {
  quit = (Button) findViewById(R.id.endBtn);
  userName = (EditText) findViewById(R.id.userName);

  Bundle extras = getIntent().getExtras();
  int score = extras.getInt("score");

  quit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String strName = userName.getText().toString();
        saveProgress(strName + ", " + score, "results.txt");
        finish();
        System.exit(0);
    }
  });
}

Calls, initializations, operations, exc, should go inside the onClick method of the listener. The onClick is fired only when the button is clicked, everything outside the onClick but inside the Listener is called on Listener initialization

Upvotes: 1

Related Questions