Jon
Jon

Reputation: 75

how to pull variable out of clickListener for use in another clickListener

what i'm trying to do is have a list of platforms for the user to choose from. by clicking a platform it puts in the name of the corresponding sql table name in a variable. but i can't figure out how to use that variable in my submit buttons clickListener. here is the section of code i am working with.

String platformText;

platform.setOnItemClickListener(new OnItemClickListener()
{
   @Override
   public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
   {
     String temp;

       if(position == 1)
       {
            temp = "ps3games";
       }
       else if(position == 2)
       {
            temp = "xbox360games";
       }
    }
});
platformText = temp;        

    submitButton.setOnClickListener(new OnClickListener()
{
     public void onClick(View v)
     {
             addProduct(platformText)
         }
    });

i've cut out code not essential to the question.

Upvotes: 1

Views: 75

Answers (1)

TronicZomB
TronicZomB

Reputation: 8747

Try this:

String platformText;

platform.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
    {
        String temp;

        if(position == 1)
        {
            temp = "ps3games";
        }
        else if(position == 2)
        {
            temp = "xbox360games";
        }
        platformText = temp; 
      }
 });

submitButton.setOnClickListener(new OnClickListener()
{
     public void onClick(View v)
     {
         addProduct(platformText)
     }
});

You needed to set platformTitle=temp within your onClickListener, you were doing that outside of it before.

Upvotes: 3

Related Questions