• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to use shared preferences in android studio?

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking how to use sharedpreferences . But It seems doesn't work at my code. First I created a global Integer for it.

After that, I have one method which is show my TxnNo.

I typed this at outside 'onCreate'


and last, when I click the button It will auto increment and also store the number.

It should be worked, But It doesn't work. I don't know why...
Saloon Keeper
Posts: 7601
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. Some points in no particular order:

1) "It doesn't work" gives us nothing to work with. What did you expect to happen, and what is happening instead?

2) Have you verified that the onClick method gets called?

3) it's confusing to have one variable called increaseNumber that is defined at the class level, and one of the same name that is a method parameter.

4) The code uses shared prefs, but you only seem to put something into them, never to get anything out of them

5) By the way, it's not "... in Android Studio" - Android Studio is an IDE; it has nothing to do with how you would use Android APIs.
Posts: 79471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know any Android, so I shall move you to our Android forum. But I can see some general Java® things:-
Don't use underscores: Java® names should be multiple words run together with subsequent words capitalised, which is often called camel case. In the case of class and interface names, the first word is capitalised, too.
Your method doesn't populate txnNo, so that is a poor name for it.
Don't use abbreviations: transaction please not Txn. Is it not obvious what some of your names mean; I cannot decipher D_Txn.
Line 3: Why are you using Integer.vaueOf() to get an int? What is wrong with this? If that method returns an int, you won't need any conversions at all.
Line 6: Don't call a String txnNo; that is a misleading name. It isn't a number. If you create that String all in one long statement rather than two small statements you will get slightly faster execution.
What does increaseNumber mean? Why are you putting it into your map in line 9? I think you need a different number there. Otherwise the rest of the method isn't doing anything.
Line 4 in the last code block hides a common mistake. Don't worry; everybody else has made that mistake.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic