1

im trying to remove values from an arrayList im my android app, but they keep re-appearing.

My arrayList is in a separate class, in my Main Activty I create an instance of that class and remove a value from the array. I exit the Main Activity and return the value re-appears.

My Question is how can I can some kind of static instance of the array class???

 //ArrayClass
 public class ArrayClass {

 public final static ArrayList<String> words = new ArrayList<String>();

 public ArrayClass() {
    words.add("WORD");
    words.add("WORD");
    words.add("WORD");
 }

 //Main Class
 ArrayClass wordc = new ArrayClass();

 @Override
 protected void onCreate(Bundle savedInstanceState) {   
      wordc.removeWord(0);
 }

3 Answers 3

2

Orest is correct you do want a singleton pattern, but remember when you access the class's methods you always need to use the getInstance() method.

For example a method within the Class:

public String getWord(index i) {
     .......
}

Should be called statically as follows

ArrayClass.getInstance().getWord(i);

NOT like this

wordc.getWord(i);

This guarantees that there is one and only one instance (thus the singleton)

Sign up to request clarification or add additional context in comments.

Comments

1

I might be confused on what you are doing but to access the static Array you don't want to create an instance of the class. Everytime you do that you are running the constructor which, in the example code, populates your static Array each time with 3 values.

I don't see exactly what you are trying to accomplish so maybe you could explain that a little better but I'm guessing this really isn't what you want your constructor doing. I think you want to access the Array itself statically

 //Main Class
 ArrayClass wordc = new ArrayClass(); 

 @Override
 protected void onCreate(Bundle savedInstanceState) {   
      wordc.removeWord(0);  //don't need this
      ArrayClass.words.remove(0);  // would remove the element at index 0
 }

But this still wouldn't solve your problem. You need a method inside your ArrayClass class that adds items to your Array. Doing it in your constructor will add these items each time you create a new instance of your class.

If this doesn't answer your question then maybe you can explain your assignment a little better.

2 Comments

Sorry about the confusion. I need to keep the state of the array trough out the life cycle of the application, so when I go back to the main the instance of the array is the same state as when I left...Thanks for the reply!
This should be the accepted answer. All those singleton answers are terrible. This isn't C++ folks, leave singletons where they belong. +1 for best response
0

Have you tried the Singleton patter? You will have one static reference of ArrayClass and it's internal state won't be violated by activity lifecycle.

public class ArrayClass {
     private static ArrayClass instance;
     public static ArrayClass getInstance() {
         if(instance == null) instance = new ArrayClass();
         return instance;
     }
//...rest goes as is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.