-1

I have 5 arrayLists created that have double values

    public ArrayList<Double> arrayList1  = new ArrayList<Double>();

    public ArrayList<Double> arrayList2 = new ArrayList<Double>();

    public ArrayList<Double> arrayList3  = new ArrayList<Double>();

    public ArrayList<Double> arrayList4  = new ArrayList<Double>();

    public ArrayList<Double> arrayList5  = new ArrayList<Double>();

I am trying to save them after the user enters values in an editText and clicks a submit button

       if(spinnerPosition==1){

           arrayList1.add(Double.parseDouble(enterText.getText().toString()));
     }

Then I would like to load arrayList and use the values inside arrayList to calculate an average. I have the method to calculate the average, just need to know how to load arrayList to calculate average every time a new value is entered

 public String arrayList1AverageResults(){

     double sum=0.0;

        if(arrayList1.size() > 0){
            for ( int i=0; i < arrayList1.size() ; i++) {

                sum += arrayList1.get(i);
            }
            arrayList1Avg = sum / arrayList1.size();
        }

        return arrayList1Avg;
    }
  • Are the ArrayList members declared in the same fragment than your arrayList1AverageResults method? If so, you should be able to access the arrays and calculate the average without having to persist them. – plgrenier Nov 23 '19 at 21:25
  • Yes they are in the same fragment, but it loses what is stored in the array when I close the app – newtoMobileDev Nov 23 '19 at 21:47
  • Check my answer below on how to persist arrayList1 when you close the app. You're gonna need to add Gson library to your app: https://github.com/google/gson. – plgrenier Nov 23 '19 at 22:06
  • why was this downvoted? – newtoMobileDev Nov 24 '19 at 19:49

3 Answers3

0

There are a number of ways to persist the data when the App close.

You could serialise them to a file.

You could serialise them to shared preferences. Save ArrayList to SharedPreferences

Or probably must better store them to a database (Android has various ways to store them to a database like SQLite

Reference https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase

Tutorial for one method https://www.tutorialspoint.com/android/android_sqlite_database.htm

Or the more "Android" way with https://developer.android.com/training/data-storage/room/

Andrew
  • 8,198
  • 2
  • 15
  • 35
0

I would persist the ArrayList using SharedPreferences using GSON library like so:

Save

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
Gson gson = new Gson();

String json = gson.toJson(arrayList1);

editor.putString("ARRAY_LIST_1", json);
editor.commit();

Read

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString("ARRAY_LIST_1", "");
arrayList1 = Arrays.asList(gson.fromJson(json, Double[].class));
plgrenier
  • 293
  • 3
  • 14
0

This is what worked for me

    private void saveArrayList1(){

        SharedPreferences sharePref= getActivity().getSharedPreferences("ArrayList1",MODE_PRIVATE);
        SharedPreferences.Editor editor= sharePref.edit();
        Gson gson= new Gson();
        String json= gson.toJson(arrayList1);
        editor.putString("Array1",json);
        editor.apply();
    }

    private void loadArray(){
        SharedPreferences sharePref= getActivity().getSharedPreferences("ArrayList1",MODE_PRIVATE);
        Gson gson= new Gson();
        String json = sharePref.getString("Array1",null);
        Type type = new TypeToken<ArrayList<Double>>(){}.getType();
        arrayList1=gson.fromJson(json,type);

        if (arrayList1 == null) {

            arrayList1 = new ArrayList<>();
        }


    }