Search code examples
androidxmlmedia-player

Passing arguments in XML for android?


Maybe I'm just going about this the wrong way, but I'm trying to have a function called playSong, and depending on the button pressed, I want to pass the parameter via xml to the playSong function so it can choose the correct song by resource id.

Is there a better way to accomplish this or is the way that I'm trying possible?


Solution

  • When passing params inside your app, it's better to send a object directly. Since sending xml/json or other text format need to parse. In this situation, you can create a param class for Button and pass it to the playSong function. If you have to pass text or String, you can also take serialization to the object.

    class ButtonParam{
        int id;
        // anything else.
    }
    
    ButtonParam param = new ButtonParam();
    param.id = 3;
    // set your param here.
    mButton.setTag(param);
    mButton.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            ButtonParam p = (ButtonParam)view.getTag();
            // call you method.
            playSong(p);
        }
    });