0

I want to store a score and name value and need to retrieve and edit it whenever a user gets higher score. How can I do that? Can somebody help me here using my code please?

MainActivity.java

package com.thesis.boggleit;


import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

 //VARIABLES HERE
  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
      
        dbHelper =new DBAdapter(this);
        search = (EditText) findViewById(R.id.textHere);
      
  OnClickListener myCommoClickListner = new OnClickListener(){

   @Override
   public void onClick(View arg0) {
          Log.i(TAG,"arg0.getId()="+arg0.getId());
                
                
                if (arg0.getId()==R.drawable.a){
                    Log.i(TAG,"arg0.getId()="+arg0.getId());
                    generatedString=generatedString+("a");
                    text.setText(generatedString);
                    ((ImageButton) arg0).setImageResource(R.drawable.changea);

            if (!timeHasStarted) {
                countDownTimer.start();
                timeHasStarted = true;
                }
         }
   
  };
  image1.setOnClickListener(myCommoClickListner);
    }
    
    //TIMER HERE
    private int optionTxtView = 0  ;
    private int addClick = 0  ;
    
    private void calculate(){
  x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
     y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
     z = x + y;
     score.setText(Integer.toString(z));
     }

     String s1= search.getText().toString();
        String s2= dbHelper.getData(s1);
      
........

}

TIA

Lab Ley
  • 151
  • 1
  • 12

3 Answers3

0

The Official Android developer guide clearly states that

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

This means you have to use the SharedPreferences class to achieve what you want.

Example:-

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); // In a fragment

SharedPreferences sharedPref = getApplicationContext().getPreferences(Context.MODE_PRIVATE); //In an Activity

Best way to learn is to visit the above link and read it.

Parth Sane
  • 587
  • 7
  • 24
0

Storing values

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name);
editor.putInt("score", score);
editor.commit();

Getting values

String name = prefs.getString("name", "");
int score = prefs.getInt("score", 0);
Kaushik
  • 6,150
  • 5
  • 39
  • 54
Phuc Tran
  • 7,555
  • 1
  • 39
  • 27
0

Use this Common class:

public static boolean setPreference(Context context, String key, int value) {

    if (context == null) {
        throw new IllegalArgumentException("'context' must not be null.");
    }
    if (key == null) {
        throw new IllegalArgumentException("'key' must not be null.");
    }

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt(key, value);
    return editor.commit();
} 
public static int getPreference(Context context, String key, int defValue) {

    if (context == null) {
        throw new IllegalArgumentException("'context' must not be null.");
    }
    if (key == null) {
        throw new IllegalArgumentException("'key' must not be null.");
    }

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getInt(key, defValue);
}

How to use :

int currentScore = 20; 
    PreferenceUtil.setPreference(context, "key1", currentScore);

    //get. If the key1 in not available, return default value 0

    int myScore = PreferenceUtil.getPreference(context, "key1", 0);
Son Nguyen Thanh
  • 1,199
  • 15
  • 19