Jason Axelrod
Jason Axelrod

Reputation: 7805

Getting variables from a custom Layout in Android?

In android, I have a custom layout I built:

public class ButtonMatch extends RelativeLayout
{
    private final TextView text_round, text_match, text_player1, text_player2;

    public ButtonMatch(final Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.button_match, this, true);

        text_round = (TextView) findViewById(R.id.text_round);
        text_match = (TextView) findViewById(R.id.text_match);
        text_player1 = (TextView) findViewById(R.id.text_player1);
        text_player2 = (TextView) findViewById(R.id.text_player2);
    }

    public void setRound(String text) {
        text_round.setText(text);
    }

    public void setMatch(String text) {
        text_match.setText(text);
    }

    public void setPlayer1(String text) {
        text_player1.setText(text);
    }

    public void setPlayer2(String text) {
        text_player2.setText(text);
    }

    public String getPlayer1() {
        return text_player1.getText();
    }

    public String getPlayer2() {
        return text_player2.getText();
    }
}

Then I am adding this layout in code with the following:

ButtonMatch button = new ButtonMatch(this);

button.setLayoutParams(layout);
button.setTag(match.get("id"));
button.setMatch(match.get("identifier").toString());
button.setPlayer1(players.get(match.get("player1_id").toString()));
button.setPlayer2(players.get(match.get("player2_id").toString()));
button.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View view)
    {
        Intent intent = new Intent(view.getContext(), ChallongeMatch.class);
        intent.putExtra(MainActivity.EXTRA_API_KEY, API_KEY);
        intent.putExtra(MainActivity.EXTRA_SUBDOMAIN, SUBDOMAIN);
        intent.putExtra(MainActivity.EXTRA_EVENT_ID, EVENT_ID);
        intent.putExtra(MainActivity.EXTRA_MATCH_ID, view.getTag().toString());
        intent.putExtra(MainActivity.EXTRA_PLAYER1, view.getPlayer1());
        intent.putExtra(MainActivity.EXTRA_PLAYER2, view.getPlayer2());
    }
});

What I am missing though, is in the Intent with the onClickListener, I am trying to get the contents of the text_player1 and text_player2 as follows:

        intent.putExtra(MainActivity.EXTRA_PLAYER1, view.getPlayer1());
        intent.putExtra(MainActivity.EXTRA_PLAYER2, view.getPlayer2());

The problem is those two functions don't work getPlayer1() and getPlayer2, because they dont exist in the view...

How do I get these two functions to work. I don't know a lot about Android/Java yet, so please be explain as much as you can.

Upvotes: 0

Views: 48

Answers (1)

David Medenjak
David Medenjak

Reputation: 34552

You forgot casting view to ButtonMatch.

This is safe, because you set the click listener on the button object.

Change your code to

((ButtonMatch) view).getPlayer1()

to access the method on your object.

Upvotes: 1

Related Questions