I want to get the top 5 integers in a shared preference file. All I have is I put 35 numbers with keys in a shared preference file and I want the top 5. I need to get them in another activity, and that is why I used shared preferences. The top 5 numbers could change, and I want the data to be saved permanently. The way the numbers change is by checkboxes, and if the user hits a checkbox the number for that key increases. If there is a better way to write the code or you have a solution, could you please write it down below?
private CheckBox yCheckbox;
private CheckBox nCheckbox;
private Button button;
public static int checker = 0;
public static final String CravingDatabse = "Cravings";
private int b;
private int toastChecker = 0;
CheckBox phone, alcohol, tv, driving, food, coffee, workBreak, sex, bed, bar, party, concert, someoneElseSmoke,
friendsWhoSmoke, tasteCigarette, cigaretteSmoke, handlingCigarettes, handsAndMouth, withdrawal, stressed,
anxious, excited, bored, sad, happy, lonely, satisfied, relaxed, location, fightingWithFriends, morningRoutine,
bills, boss, arguing, bigEvent;
int phoneN, alcoholN, tvN, drivingN, foodN, coffeeN, workBreakN, sexN, bedN, barN, partyN, concertN, someoneElseSmokeN,
friendsWhoSmokeN, tasteCigaretteN, cigaretteSmokeN, handlingCigarettesN, handsAndMouthN, withdrawalN, stressedN,
anxiousN, excitedN, boredN, sadN, happyN, lonelyN, satisfiedN, relaxedN, locationN, fightingWithFriendsN, morningRoutineN,
billsN, bossN, arguingN, bigEventN;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smoking_craving);
SharedPreferences prefs = getSharedPreferences("huan", MODE_PRIVATE);
b = prefs.getInt("amount", 0);
final SharedPreferences sharedPreferences = getSharedPreferences(CravingDatabse, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
button = (Button) findViewById(R.id.cravingCheck);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checker == 1) {
//MainActivity.b = b + 1;
//checker = 0;
b = b + 1;
SharedPreferences prefs = getSharedPreferences("huan", MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs.edit();
editor1.putInt("amount", b);
editor1.apply();
}
if (toastChecker == 1) {
Toast.makeText(getApplicationContext(), "Way to go!",
Toast.LENGTH_LONG).show();
toastChecker = 0;
int urgesPassed = sharedPreferences.getInt("urgesPassed", 0);
editor.putInt("urgesPassed", ++urgesPassed);
editor.apply();
}
if (alcoholN == 1) {
int alcoholS = sharedPreferences.getInt("alcoholS", 0);
editor.putInt("alcoholS", ++alcoholS);
editor.apply();
}
if (phoneN == 1) {
int phoneS = sharedPreferences.getInt("phoneS", 0);
editor.putInt("phoneS", ++phoneS);
editor.apply();
}
if (tvN == 1) {
int tvS = sharedPreferences.getInt("tvS", 0);
editor.putInt("tvS", ++tvS);
editor.apply();
}
if (drivingN == 1) {
int drivingS = sharedPreferences.getInt("drivingS", 0);
editor.putInt("drivingS", ++drivingS);
editor.apply();
}
if (foodN == 1) {
int foodS = sharedPreferences.getInt("foodS", 0);
editor.putInt("foodS", ++foodS);
editor.apply();
}
if (coffeeN == 1) {
int coffeeS = sharedPreferences.getInt("coffeeS", 0);
editor.putInt("coffeeS", ++coffeeS);
editor.apply();
}
if (workBreakN == 1) {
int workBreakS = sharedPreferences.getInt("workBreakS", 0);
editor.putInt("workBreakS", ++workBreakS);
editor.apply();
}
if (sexN == 1) {
int sexS = sharedPreferences.getInt("sexS", 0);
editor.putInt("sexS", ++sexS);
editor.apply();
}
if (bedN == 1) {
int bedS = sharedPreferences.getInt("bedS", 0);
editor.putInt("bedS", ++bedS);
editor.apply();
}
if (barN == 1) {
int barS = sharedPreferences.getInt("barS", 0);
editor.putInt("barS", ++barS);
editor.apply();
}
if (partyN == 1) {
int partyS = sharedPreferences.getInt("partyS", 0);
editor.putInt("partyS", ++partyS);
editor.apply();
}
if (concertN == 1) {
int concertS = sharedPreferences.getInt("concertS", 0);
editor.putInt("concertS", ++concertS);
editor.apply();
}
if (someoneElseSmokeN == 1) {
int someoneElseSmokeS = sharedPreferences.getInt("someoneElseSmokeS", 0);
editor.putInt("someoneElseSmokeS", ++someoneElseSmokeS);
editor.apply();
}
if (friendsWhoSmokeN == 1) {
int friendsWhoSmokeS = sharedPreferences.getInt("friendsWhoSmokeS", 0);
editor.putInt("friendsWhoSmokeS", ++friendsWhoSmokeS);
editor.apply();
}
if (tasteCigaretteN == 1) {
int tasteCigaretteS = sharedPreferences.getInt("tasteCigaretteS", 0);
editor.putInt("tasteCigaretteS", ++tasteCigaretteS);
editor.apply();
}
if (cigaretteSmokeN == 1) {
int cigaretteSmokeS = sharedPreferences.getInt("cigaretteSmokeS", 0);
editor.putInt("cigaretteSmokeS", ++cigaretteSmokeS);
editor.apply();
}
if (handlingCigarettesN == 1) {
int handlingCigarettesS = sharedPreferences.getInt("handlingCigarettesS", 0);
editor.putInt("handlingCigarettesS", ++handlingCigarettesS);
editor.apply();
}
if (handsAndMouthN == 1) {
int handsAndMouthS = sharedPreferences.getInt("handsAndMouthS", 0);
editor.putInt("handsAndMouthS", ++handsAndMouthS);
editor.apply();
}
if (withdrawalN == 1) {
int withdrawalS = sharedPreferences.getInt("withdrawalS", 0);
editor.putInt("withdrawalS", ++withdrawalS);
editor.apply();
}
if (stressedN == 1) {
int stressedS = sharedPreferences.getInt("stressedS", 0);
editor.putInt("stressedS", ++stressedS);
editor.apply();
}
if (anxiousN == 1) {
int anxiousS = sharedPreferences.getInt("anxiousS", 0);
editor.putInt("anxiousS", ++anxiousS);
editor.apply();
}
if (excitedN == 1) {
int excitedS = sharedPreferences.getInt("excitedS", 0);
editor.putInt("excitedS", ++excitedS);
editor.apply();
}
if (boredN == 1) {
int boredS = sharedPreferences.getInt("boredS", 0);
editor.putInt("boredS", ++boredS);
editor.apply();
}
if (sadN == 1) {
int sadS = sharedPreferences.getInt("sadS", 0);
editor.putInt("sadS", ++sadS);
editor.apply();
}
if (happyN == 1) {
int happyS = sharedPreferences.getInt("happyS", 0);
editor.putInt("happyS", ++happyS);
editor.apply();
}
if (lonelyN == 1) {
int lonelyS = sharedPreferences.getInt("lonelyS", 0);
editor.putInt("lonelyS", ++lonelyS);
editor.apply();
}
if (satisfiedN == 1) {
int satisfiedS = sharedPreferences.getInt("satisfiedS", 0);
editor.putInt("satisfiedS", ++satisfiedS);
editor.apply();
}
if (relaxedN == 1) {
int relaxedS = sharedPreferences.getInt("relaxedS", 0);
editor.putInt("relaxedS", ++relaxedS);
editor.apply();
}
if (locationN == 1) {
int locationS = sharedPreferences.getInt("locationS", 0);
editor.putInt("locationS", ++locationS);
editor.apply();
}
if (fightingWithFriendsN == 1) {
int fightingWithFriendsS = sharedPreferences.getInt("fightingWithFriendsS", 0);
editor.putInt("fightingWithFriendsS", ++fightingWithFriendsS);
editor.apply();
}
if (morningRoutineN == 1) {
int morningRoutineS = sharedPreferences.getInt("morningRoutineS", 0);
editor.putInt("morningRoutineS", ++morningRoutineS);
editor.apply();
}
if (billsN == 1) {
int billsS = sharedPreferences.getInt("billsS", 0);
editor.putInt("billsS", ++billsS);
editor.apply();
}
if (bossN == 1) {
int bossS = sharedPreferences.getInt("bossS", 0);
editor.putInt("bossS", ++bossS);
editor.apply();
}
if (arguingN == 1) {
int arguingS = sharedPreferences.getInt("arguingS", 0);
editor.putInt("arguingS", ++arguingS);
editor.apply();
}
if (bigEventN == 1) {
int bigEventS = sharedPreferences.getInt("bigEventS", 0);
editor.putInt("bigEventS", ++bigEventS);
editor.apply();
}
Intent intent = new Intent(SmokingAndCravingCheck.this, MainActivity.class);
SmokingAndCravingCheck.this.startActivity(intent);
finish();
}
});
yCheckbox = (CheckBox) findViewById(R.id.yesCheckBox);
nCheckbox = (CheckBox) findViewById(R.id.noCheckBox);
yCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yCheckbox.isChecked()) {
nCheckbox.setChecked(false);
checker = 1;
toastChecker = 0;
} else if (!yCheckbox.isChecked()) {
checker = 0;
}
}
});
nCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (nCheckbox.isChecked()) {
yCheckbox.setChecked(false);
toastChecker = 1;
checker = 0;
} else if (!nCheckbox.isChecked()) {
toastChecker = 0;
}
}
});
phone = (CheckBox) findViewById(R.id.onPhoneCheckBtn);
phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phone.isChecked()) {
phoneN = 1;
} else if (!phone.isChecked()) {
phoneN = 0;
}
}
});
alcohol = (CheckBox) findViewById(R.id.alcoholCheckBtn);
alcohol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (alcohol.isChecked()) {
alcoholN = 1;
} else if (!alcohol.isChecked()) {
alcoholN = 0;
}
}
});
tv = (CheckBox) findViewById(R.id.tvCheckBtn);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (tv.isChecked()) {
tvN = 1;
} else if (!tv.isChecked()) {
tvN = 0;
}
}
});
driving = (CheckBox) findViewById(R.id.drivingCheckBtn);
driving.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (driving.isChecked()) {
drivingN = 1;
} else if (!driving.isChecked()) {
drivingN = 0;
}
}
});
food = (CheckBox) findViewById(R.id.mealCheckBtn);
food.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (food.isChecked()) {
foodN = 1;
} else if (!food.isChecked()) {
foodN = 0;
}
}
});
coffee = (CheckBox) findViewById(R.id.cofeeCheckBtn);
coffee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (coffee.isChecked()) {
coffeeN = 1;
} else if (!coffee.isChecked()) {
coffeeN = 0;
}
}
});
workBreak = (CheckBox) findViewById(R.id.workCheckBtn);
workBreak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (workBreak.isChecked()) {
workBreakN = 1;
} else if (!workBreak.isChecked()) {
workBreakN = 0;
}
}
});
sex = (CheckBox) findViewById(R.id.sexCheckBtn);
sex.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sex.isChecked()) {
sexN = 1;
} else if (!sex.isChecked()) {
sexN = 0;
}
}
});
bed = (CheckBox) findViewById(R.id.bedCheckBtn);
bed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bed.isChecked()) {
bedN = 1;
} else if (!bed.isChecked()) {
bedN = 0;
}
}
});
bar = (CheckBox) findViewById(R.id.barCheckBtn);
bar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bar.isChecked()) {
barN = 1;
} else if (!bar.isChecked()) {
barN = 0;
}
}
});
party = (CheckBox) findViewById(R.id.partyCheckBtn);
party.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (party.isChecked()) {
partyN = 1;
} else if (!party.isChecked()) {
partyN = 0;
}
}
});
concert = (CheckBox) findViewById(R.id.concertCheckBtn);
concert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (concert.isChecked()) {
concertN = 1;
} else if (!concert.isChecked()) {
concertN = 0;
}
}
});
someoneElseSmoke = (CheckBox) findViewById(R.id.someoneElseSmokeCheckBtn);
someoneElseSmoke.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (someoneElseSmoke.isChecked()) {
someoneElseSmokeN = 1;
} else if (!someoneElseSmoke.isChecked()) {
someoneElseSmokeN = 0;
}
}
});
friendsWhoSmoke = (CheckBox) findViewById(R.id.friendsCheckBtn);
friendsWhoSmoke.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (friendsWhoSmoke.isChecked()) {
friendsWhoSmokeN = 1;
} else if (!friendsWhoSmoke.isChecked()) {
friendsWhoSmokeN = 0;
}
}
});
tasteCigarette = (CheckBox) findViewById(R.id.tasteCigaretteCheckBtn);
tasteCigarette.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (tasteCigarette.isChecked()) {
tasteCigaretteN = 1;
} else if (!tasteCigarette.isChecked()) {
tasteCigaretteN = 0;
}
}
});
cigaretteSmoke = (CheckBox) findViewById(R.id.smokeCheckBtn);
cigaretteSmoke.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cigaretteSmoke.isChecked()) {
cigaretteSmokeN = 1;
} else if (!cigaretteSmoke.isChecked()) {
cigaretteSmokeN = 0;
}
}
});
handlingCigarettes = (CheckBox) findViewById(R.id.handlingCheckBtn);
handlingCigarettes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (handlingCigarettes.isChecked()) {
handlingCigarettesN = 1;
} else if (!handlingCigarettes.isChecked()) {
handlingCigarettesN = 0;
}
}
});
handsAndMouth = (CheckBox) findViewById(R.id.handsCheckBtn);
handsAndMouth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (handsAndMouth.isChecked()) {
handsAndMouthN = 1;
} else if (!handsAndMouth.isChecked()) {
handsAndMouthN = 0;
}
}
});
withdrawal = (CheckBox) findViewById(R.id.withdrawalCheckBtn);
withdrawal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (withdrawal.isChecked()) {
withdrawalN = 1;
} else if (!withdrawal.isChecked()) {
withdrawalN = 0;
}
}
});
stressed = (CheckBox) findViewById(R.id.stressedCheckBtn);
stressed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (stressed.isChecked()) {
stressedN = 1;
} else if (!stressed.isChecked()) {
stressedN = 0;
}
}
});
anxious = (CheckBox) findViewById(R.id.anxiousCheckBtn);
anxious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (anxious.isChecked()) {
anxiousN = 1;
} else if (!anxious.isChecked()) {
anxiousN = 0;
}
}
});
excited = (CheckBox) findViewById(R.id.excitedCheckBtn);
excited.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (excited.isChecked()) {
excitedN = 1;
} else if (!excited.isChecked()) {
excitedN = 0;
}
}
});
bored = (CheckBox) findViewById(R.id.boredCheckBtn);
bored.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bored.isChecked()) {
boredN = 1;
} else if (!bored.isChecked()) {
boredN = 0;
}
}
});
sad = (CheckBox) findViewById(R.id.sadCheckBtn);
sad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sad.isChecked()) {
sadN = 1;
} else if (!sad.isChecked()) {
sadN = 0;
}
}
});
happy = (CheckBox) findViewById(R.id.happyCheckBtn);
happy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (happy.isChecked()) {
happyN = 1;
} else if (!happy.isChecked()) {
happyN = 0;
}
}
});
lonely = (CheckBox) findViewById(R.id.lonelyCheckBtn);
lonely.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (lonely.isChecked()) {
lonelyN = 1;
} else if (!lonely.isChecked()) {
lonelyN = 0;
}
}
});
satisfied = (CheckBox) findViewById(R.id.satisfiedCheckBtn);
satisfied.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (satisfied.isChecked()) {
satisfiedN = 1;
} else if (!satisfied.isChecked()) {
satisfiedN = 0;
}
}
});
relaxed = (CheckBox) findViewById(R.id.relaxedCheckBtn);
relaxed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (relaxed.isChecked()) {
relaxedN = 1;
} else if (!relaxed.isChecked()) {
relaxedN = 0;
}
}
});
location = (CheckBox) findViewById(R.id.locationCheckBtn);
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (location.isChecked()) {
locationN = 1;
} else if (!location.isChecked()) {
locationN = 0;
}
}
});
fightingWithFriends = (CheckBox) findViewById(R.id.fightingWithFriendsCheckBtn);
fightingWithFriends.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (fightingWithFriends.isChecked()) {
fightingWithFriendsN = 1;
} else if (!fightingWithFriends.isChecked()) {
fightingWithFriendsN = 0;
}
}
});
morningRoutine = (CheckBox) findViewById(R.id.morningRoutineCheckBtn);
morningRoutine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (morningRoutine.isChecked()) {
morningRoutineN = 1;
} else if (!morningRoutine.isChecked()) {
morningRoutineN = 0;
}
}
});
bills = (CheckBox) findViewById(R.id.payingBillsCheckBtn);
bills.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bills.isChecked()) {
billsN = 1;
} else if (!bills.isChecked()) {
billsN = 0;
}
}
});
boss = (CheckBox) findViewById(R.id.bossCheckBtn);
boss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (boss.isChecked()) {
bossN = 1;
} else if (!boss.isChecked()) {
bossN = 0;
}
}
});
arguing = (CheckBox) findViewById(R.id.arguingCheckBtn);
arguing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (arguing.isChecked()) {
arguingN = 1;
} else if (!arguing.isChecked()) {
arguingN = 0;
}
}
});
bigEvent = (CheckBox) findViewById(R.id.bigEventCheckBtn);
bigEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bigEvent.isChecked()) {
bigEventN = 1;
} else if (!bigEvent.isChecked()) {
bigEventN = 0;
}
}
});
}
}