Dusan
Dusan

Reputation: 3478

Changing a Button's text inside a ListView in Android

I have a custom ListView set up with custom ArrayAdapter. One row is made of 10 buttons. I'm not able to change the buttons caption after clicking the button. Inside the getView method i'm seeting up a holder for all my buttons. The click listener is in the main activity, it is working correctly (i think so), i'm able to get a reference of the button:

MyHolder h = (MyHolder) getListView().getAdapter().getView(position, null, null).getTag();

Button b = h.myButton;

now when i call b.getText(), it gives me the clicked button's text. But when i try: b.setText("xxx"); the button's text doesnt change.

any ideas?

Upvotes: 4

Views: 1714

Answers (1)

Sam
Sam

Reputation: 86948

I don't think that calling: getListView().getAdapter().getView(position, null, null) manually actually returns the existing View at position. This only creates a new View with the same data, which is why you don't see any changes and don't receive any errors.

Simply use the View passed in the Button's OnClickListener to change its own text.

public void onClick(View v) {
    Button b = (Button) v;
    b.setText(...);
}

Upvotes: 3

Related Questions