简体   繁体   中英

how to store every integer value in array list when the value is change every time Activity is open?

This value is change every time when is activity is open

int seeProductID = Integer.parseInt(getIntent().getStringExtra("Id"));

Creating a arraY list type is integer

 ArrayList<Integer> list=new ArrayList<Integer>();

Add the value to the list

list.add(seeProductID );

System.out.print(list);

But the value is add is only one and replace the value to the last one. How to create a arraylist which is store every change seeProductID value when the activity load.

Please try this way may help you, if your are only using this list during the application open then you can create arraylist varibale or save in application class which is common for all activity,but this list does not save data when you close the application and open again......

So If you want to save this even app close or open until app installed in your device or no mannualy clear data then GOTO BELOW Easy and memory saving way may help you.

In Manifest file

<application
    android:name=".YourApplicationClass"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:allowBackup">

    </application>

In yourApplication Class

public static class YourApplicationClass extends Application{
        private static YourApplicationClass instance;
        ArrayList<Integer> arrayList = null;
        private void init(Application app) {
            instance = this;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            init(this);
        }
    }

In your Activity1 Class

public class MyActivity1{
        public void launch(){
            Intent intent = new Intent(this,MyActivity2.class);
            intent.putExtra("Id","22");
            startActivity(intent);
        }

    }

In Your Activity2 Class

public class MyActivity2{
        String keyID = "";
        int seeProductID = 0;
        public void oncreate(){
            seeProductID  = Integer.parseInt(getIntent().getStringExtra("Id"));
            getTheSavedListData();
        }

        public void getTheSavedListData(){
            keyID = SharedPreferenceManager.getInstance(this).get(SharedPreferenceManager.ListKey);
            if(keyID != null){
                YourApplicationClass.instance.arrayList = new ArrayList<>();
                String[] arrayString = keyID.split(SharedPreferenceManager.ListKeyDelimeter);
                for(int i=0;i<arrayString.length;i++){
                    if(!isStringNullOrBlank(arrayString[i]) && !arrayString[i].contains(SharedPreferenceManager.ListKeyDelimeter)){
                        YourApplicationClass.instance.arrayList.add(Integer.parseInt(arrayString[i]));
                    }
                }
            }
        }

        /*from where you want to save its depend on you*/
        public void onDestroy(){

            saveTheDataDirectByID();//1
              /*OR*/
            saveTheDataByList();//2

        }


        /*Method 1 for save*/
        public void saveTheDataDirectByID(){
            if(isStringNullOrBlank(keyID)){
                keyID = ""+seeProductID;
            }else{
                keyID = keyID+SharedPreferenceManager.ListKeyDelimeter+seeProductID;
            }
            SharedPreferenceManager.getInstance(this).save(SharedPreferenceManager.ListKey,keyID);
        }

        /*Method 2 for save above and this method both works same..you can use any one*/
        public void saveTheDataByList(){
            String keyid = "";
            for(int i=0;i<YourApplicationClass.instance.arrayList.size();i++){
                if(isStringNullOrBlank(keyid)){
                    keyid = ""+YourApplicationClass.instance.arrayList.get(i);
                }else{
                    keyid = keyid+SharedPreferenceManager.ListKeyDelimeter+YourApplicationClass.instance.arrayList.get(i);
                }
            }
            SharedPreferenceManager.getInstance(this).save(SharedPreferenceManager.ListKey,keyid);
        }

        public boolean isStringNullOrBlank(String str) {
            try{
                if (str == null) {
                    return true;
                } else if (str.equals("null") || str.equals("") || (str != null && str.isEmpty()) || (str != null && str.length() <= 0) || str.equalsIgnoreCase("null")) {
                    return true;
                }

            }catch(Exception e){

                ExceptionHandler.printStackTrace(e);
            }
            return false;
        }

    }

SharedPreference Utility Class

public static class SharedPreferenceManager {
        private static final String Your_preference_name = "Your_preference_name";
        private static SharedPreferenceManager instance;
        Context context;
        private SharedPreferences sharedPreferences;

        public static String ListKey = "ListKey";
        public static String ListKeyDelimeter = "-";

        public static synchronized SharedPreferenceManager getInstance(Context context) {
            if (instance == null) {
                instance = new SharedPreferenceManager(context);
            }
            return instance;
        }

        private SharedPreferenceManager(Context context) {
            instance = this;
            this.context = context;
            if (TaxiappApplication.getInstance() != null) {
                sharedPreferences = context.getSharedPreferences(Your_preference_name, Context.MODE_PRIVATE);
            }
        }

        public void save(String key, Object value) {
            try {
                SharedPreferences.Editor editor = getEditor();
                if (value instanceof Boolean) {
                    editor.putBoolean(key, (Boolean) value);
                } else if (value instanceof Integer) {
                    editor.putInt(key, (Integer) value);
                } else if (value instanceof Float) {
                    editor.putFloat(key, (Float) value);
                } else if (value instanceof Long) {
                    editor.putLong(key, (Long) value);
                } else if (value instanceof String) {
                    editor.putString(key, (String) value);
                } else if (value instanceof Enum) {
                    editor.putString(key, value.toString());
                } else if (value != null) {
                    throw new RuntimeException("Attempting to save non-supported preference");
                }

                editor.commit();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        private SharedPreferences.Editor getEditor() {
            return sharedPreferences.edit();
        }

        @SuppressWarnings("unchecked")
        public <T> T get(String key, T defValue) {
            T returnValue = (T) sharedPreferences.getAll().get(key);
            return returnValue == null ? defValue : returnValue;
        }

        public boolean has(String key) {
            return sharedPreferences.contains(key);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM