0

I'm trying to update a button's text by using an intent with a subactivity to debug why my player was throwing an exception, but it returns null until I call the subactivity again where it updates with the result of the first call.

Main Activity

public class MainActivity extends AppCompatActivity {

    static final int PICK_AUDIO_REQUEST = 1;
    String ResultPath;
    String path;
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Sound sound = new Sound(R.id.button,this);

        sound.button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(sound.mp == null){
                    Intent intent = new Intent(MainActivity.this, Pop.class);
                    startActivityForResult(intent, PICK_AUDIO_REQUEST);
                    //Pass audio file into sound
                    /*try {
                        sound.mp.setDataSource(path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                }
                else if(sound.mp.isPlaying()) {
                    sound.mp.pause();
                }
                else{
                    sound.mp.seekTo(0);
                    sound.mp.start();
                }
            }


        });



    }

Subactivity

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("song path", mMusicList[arg2]);
                setResult(Activity.RESULT_OK, resultIntent);
                finish();
            }
        });

Example:

Intialization:

Subroutine is called and I select ring.wav from the popup

Afterwards, selecting ring.wav will not update the button with the correct path until you click on the button again.

onActivityResult

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case (PICK_AUDIO_REQUEST) : {
                if (resultCode == Activity.RESULT_OK) {

                    ResultPath = data.getStringExtra("song path");
                    path = extStorageDirectory + File.separator + ResultPath;

                }
                break;
            }
        }
    }
Sterisk
  • 63
  • 9

2 Answers2

0

I need to see more code (e.g.) onActivityResult, where is path variable, etc.

So far, I believe that on the onActivityResult you should update the text.

Check here.

Amg91
  • 165
  • 8
  • 25
0

Maybe, you add "button.setText()" in onActivityResult.

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (PICK_AUDIO_REQUEST) : {
            if (resultCode == Activity.RESULT_OK) {

                ResultPath = data.getStringExtra("song path");
                path = extStorageDirectory + File.separator + ResultPath;
                //Change button with audio file's path
                sound.button.setText(path);

            }
            break;
        }
    }
}
amuyu
  • 236
  • 1
  • 2
  • 9