1

I am quite new to Java so please forgive me if this is a silly question. I have an Android project and it contains classes which are linked, let me try break this down...

  • MainActivity (where user will enter values from EditText fields, and then the data needs to be stored somewhere).
  • CustomUseCase (this class will need to access this data, and use the data from it to do a calculation for a test).
  • ParticleActivity (user will enter more data here, that data needs to be retrieved by CustomUseCase and then theres a button that will run CustomUseCase test
  • SolutionActivity(results of these tests will be shown here).

Now the data the user inputs needs to be stored in a String list and 3 Double lists. I know how to implement this but I want to know what is the best way to organise this as I am quite new to Object-Oriented programming. Normally I would make these lists static and declare them on the MainActivity, get the user input and access it via the static variables. But I want to try a ore OO approach so I want to build a model where I can get these costs, have it saved somewhere and then retrieve it without having any memory leak etc. I created a class called UserInputSet, here is my class...

public class UserInputSet {

//user inputs 3 costs from 3 edittext fields and each one gets saved in each of these
List<Double> DATAcost = new ArrayList<Double>();
List<Double> WLANcost = new ArrayList<Double>();
List<Double> UTILITYcost = new ArrayList<Double>();

//user enters string names for 1 set of costs
List<String> serviceNames = new ArrayList<String>();
List<String> dimensions = new ArrayList<String>(String);

}

The layout of the MainActivity presents the user with 4 edittext fields displayed horizontally, the first being the String (name), and the next 3 being each cost (Datacost, WLANcost, UtilityCost) and then they should be saved here, and then retrieved by CustomUseCase. What is the best way to do this? Thank you :)

1
  • Are you looking for the import java.io.File? if so, here is a link with a tutorial: Java.io.File Class Commented May 1, 2017 at 18:56

2 Answers 2

1

based on your question , you need to organise Data with Oops concept. so better try singleton for UserInputSet class.

Singleton:

Singleton Pattern says that just"define a class that has only one instance and provides a global point of access to it".

Advantage :

  • We can make constructor as private. So that We can not create an object outside of the class.

  • This property is useful to create singleton class in java.

  • Singleton pattern helps us to keep only one instance of a class at any time.

  • The purpose of singleton is to control object creation by keeping private constructor.

UserInputSet.java

    public class UserInputSet
{
    private List<Double> mDataCost = new ArrayList(); 
    private static UserInputSet mUserInputSet = new UserInputSet(); //Object as static
    private UserInputSet()
    {
        // Empty Constructor declared as private so this class can nt be create object outside class.
    }

    public List<Double> getDataCost()
    {
        return mDataCost;
    }

    private void setData(List<Double> list)
    {
        if(list != null)
        {
            mList.clear();
            mList.addAll(list);
        }
    }

    public static UserInputSet getThis()
    {
        return mUserInputSet;
    }
}

To call UserInputSet class method's from outside Activity :

MainActivity.java

     UserInputSet userInputSet = UserInputSet.getThis();
     userInputSet.getDataCost();
Sign up to request clarification or add additional context in comments.

11 Comments

As I mentioned in my answer, this approach won't work well due to the somewhat transient nature of Android applications. If the app gets killed while in the background, the static variables will be null when the app is restarted and brought to the foreground. You need to arrange for some sort of persistence.
@GreyBeardedGeek they set variable as static this make memory leak so i told to create singleton for UserInputSet. what wrong on that ? , just i share best way to store data when app live.
@GreyBeardedGeek, can you pls explain clearly , Why my answer downvoted ?
I like this answer thats why I gave it an up again. I respect both your views but this is more what I was looking for. So if I use these methods in the mainactivity, and add each one to an arraylist, how do I access it in CustomUseCase ?
simply call getDataCost() method in CustomUsecase class same like MainActivity
|
1

This sort of use of static variables is not a good solution from an Object-Oriented point of view, and is not an idiomatic approach in Java in general, nor in Android.

Also, while Activities are one of the most prominent classes in Android development, they are not meant to be data sources, nor are they really good places to put much in the way of application logic. They are essentially view controllers.

Shared State (your lists) should be wrapped in a service class (not necessarily an Android Service), and usually, one would put that data into the (SQLite) database.

It may seem like you could do without the database, but Android applications are somewhat transient - if you put the app into the background (e.g. open another app), the OS is free to kill your app, and restart it later. So even for what the user perceives as a single run of your app, you probably need persistent data, and the database is the usual way of achieving that.

1 Comment

thank you for this response, the thing is I won't need the app to be running at all on the device. It is purely for testing purposes on how well a swarm-particle algorithm works on a mobile device. I appreciate your response, however you are right, I could do without the database. These lists are going to passed onto a fitness function of the PSO algorithm to work out a bit combination of the user input. Is there a way where I can just use a model that adds/retrieves data to an arraylist and then access these from another class? without it being an activity

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.