Providing argument to method called from OnClickListener

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Providing argument to method called from OnClickListener

Postby Wubinator » Sun Jun 21, 2009 12:36 pm

Hi all,

I have a question, I'm trying to figure out how to provide an argument to a method that is called from within a OnClickListener.

I retrieve a list of characters from a website and when they are retrieved I'm creating a button for each of the characters.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. for (Character character : characterList) {
  2.  
  3.                         Button button = new Button(this);
  4.  
  5.                         button.setText(character.name);
  6.  
  7.  
  8.  
  9.                         // Create a click listener for the button
  10.  
  11.                         button.setOnClickListener(new View.OnClickListener() {
  12.  
  13.                                 @Override
  14.  
  15.                                 public void onClick(View v) {
  16.  
  17.                                         showCharacterDetails();
  18.  
  19.                                 }
  20.  
  21.                         });
  22.  
  23.  
  24.  
  25.                         layout.addView(button);
  26.  
  27.                 }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Now what I want to do is provide the 'character' to showCharacterDetails so this method knows for which character it needs to show the details, but I can't find out where to get it from within the OnClickListener.

Is there a way to do this?

Cheers,
Rick
Wubinator
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 24, 2009 9:24 pm
Location: Amsterdam

Top

Postby pentace » Sun Jun 21, 2009 1:02 pm

What exactly are the character details? I dont quite understand what u are looking for.
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby Wubinator » Sun Jun 21, 2009 2:02 pm

The method showCharacterDetails(...) is just a method that start another Activity using Intent

Below is the method I want to call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void showCharacterInformation(int characterID) {
  2.  
  3.                 Intent i = new Intent(this, CharacterInformation.class);
  4.  
  5.                 startActivity(i);
  6.  
  7.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The characterID is an attribute found inside 'character' (just as character.name which is used inside button.setText(character.name)

So what I want to do is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // Create a button for each  of the characters inside the characterList
  3.  
  4. for (Character character : characterList) {
  5.  
  6.                Button button = new Button(this);
  7.  
  8.                button.setText(character.name);
  9.  
  10.  
  11.  
  12.                // Create a click listener for the button
  13.  
  14.                button.setOnClickListener(new View.OnClickListener() {
  15.  
  16.                     @Override
  17.  
  18.                     public void onClick(View v) {
  19.  
  20.                          showCharacterDetails(character.characterID); // Provide the attribute characterID to the method
  21.  
  22.                     }
  23.  
  24.                });
  25.  
  26.  
  27.  
  28.                layout.addView(button);
  29.  
  30.           }
  31.  
  32.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


But inside the onClick method I don't have a reference to character (well at least I can't find it)

If there is a totally different approach to do something like this I would love to hear about it too :)
Wubinator
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 24, 2009 9:24 pm
Location: Amsterdam

Postby tbi » Sun Jun 21, 2009 10:21 pm

I am still not too sure what you're trying to accomplish, but you can reference the respective instances to a class. For example, you could get a button's caption in it's listener by typing "Button.this.getText();". Maybe that might help you.

Greets,
/tbi
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Postby nmc » Mon Jun 22, 2009 1:18 am

Now what I want to do is provide the 'character' to showCharacterDetails so this method knows for which character it needs to show the details, but I can't find out where to get it from within the OnClickListener.

Is there a way to do this?


I didnt tested this, but a quick look into the reference gave me the getId()/setId() pair;
maybe you could store an index to your character-list while creating the buttons using setId() and get it back (using getId()) in your click-listener?
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby Wubinator » Mon Jun 22, 2009 7:51 am

I didnt tested this, but a quick look into the reference gave me the getId()/setId() pair;
maybe you could store an index to your character-list while creating the buttons using setId() and get it back (using getId()) in your click-listener?


That sounds like an idea I could use .... I'll try it out tonight thanks!
Wubinator
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 24, 2009 9:24 pm
Location: Amsterdam

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 7 guests