Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the MIT license.

import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashSet;

import java.util.Set;

public class Main {
    public static void setPersonGroupName(String personGroupIdToAdd, String personGroupName, Context context) {
        SharedPreferences personGroupIdNameMap = context.getSharedPreferences("PersonGroupIdNameMap",
                Context.MODE_PRIVATE);

        SharedPreferences.Editor personGroupIdNameMapEditor = personGroupIdNameMap.edit();
        personGroupIdNameMapEditor.putString(personGroupIdToAdd, personGroupName);
        personGroupIdNameMapEditor.commit();

        Set<String> personGroupIds = getAllPersonGroupIds(context);
        Set<String> newPersonGroupIds = new HashSet<>();
        for (String personGroupId : personGroupIds) {
            newPersonGroupIds.add(personGroupId);
        }
        newPersonGroupIds.add(personGroupIdToAdd);
        SharedPreferences personGroupIdSet = context.getSharedPreferences("PersonGroupIdSet", Context.MODE_PRIVATE);
        SharedPreferences.Editor personGroupIdSetEditor = personGroupIdSet.edit();
        personGroupIdSetEditor.putStringSet("PersonGroupIdSet", newPersonGroupIds);
        personGroupIdSetEditor.commit();
    }

    public static Set<String> getAllPersonGroupIds(Context context) {
        SharedPreferences personGroupIdSet = context.getSharedPreferences("PersonGroupIdSet", Context.MODE_PRIVATE);
        return personGroupIdSet.getStringSet("PersonGroupIdSet", new HashSet<String>());
    }
}