Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9059683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:02:22+00:00 2026-06-16T15:02:22+00:00

I have a separate class for my OnClickListeners. I would like to add items

  • 0

I have a separate class for my OnClickListeners. I would like to add items to an arraylist when i click a button, and remove them when I click a 2nd time. I have the framework here:

public void onClick(View v) {
        Button button = (Button)v;
        if(isClicked) {
            button.setText("Enabled");
            Log.v("Spirit: ", v.getTag() + "");
            spirits_list.add(v.getTag() + "");
            isClicked = false;
        } else {
            button.setText("Disabled");
            spirits_list.remove(v.getId()-1);
            isClicked = true;
        }



    }

I also have an ArrayList initialized at the top, but every time I click a button it reinitializes the ArrayList. How can I get around this? Also, I need to be able to save the ArrayList to SharedPreferences – how can I do this from my OnClickListener?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:02:22+00:00Added an answer on June 16, 2026 at 3:02 pm

    Since you use your own class, you can pass all the goodness around in class members:

    class MyListener implements View.OnClickListener {
    
        private ArrayList<Object> spirits_list;
        private Context ctx;
    
        public MyListener( Context ctx, ArrayList<Object> list ) {
            super();
            this.ctx = ctx;
            this.spirits_list = list;
        }
    
        public void onClick(View v) {
            Button button = (Button)v;
            if(isClicked) {
                button.setText("Enabled");
                Log.v("Spirit: ", v.getTag() + "");
                this.spirits_list.add(v.getTag() + "");
                isClicked = false;
            } else {
                button.setText("Disabled");
                this.spirits_list.remove(v.getId()-1);
                isClicked = true;
            }
    
    
    
        }
    
    }
    

    The context will allow you to access SharedPrefs as well. You can then invoke this bit as follows:

    view.setOnClickListener( new MyListener( this, spirits_list ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three buttons in my Activity with three separate onClickListeners set like I
I have created a separate class (let's call it class2.cs for example) and want
I have a background worker which calls a function within a separate class. This
Say I have two separate classes, A and B. I also have Repository class
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)
I have a list of class items List<MyClass> I have a seperate object that
I have two comma-separated selectors; .class, .foo, .bar .lorem, .ipsum, .potato I'd like to
I have a pyinotify watcher running threaded, called as a separate class, at the
So I have a view and in my viewcontoller class I try and add
I have a separate class for each of my database entities and when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.