-1

I have list of Installed apps, When i click any of them i can get the package name of clicked App, Now I want to Save the clicked package name in an ArrayList of String to SharedPreferences with Gson. and fetch the saved package name.

I checked some of previous answers but i didn't get the solution i wanted.

This is the code which i want to save the package name of clicked app.

   String ApplicationPackageName =  adapter.stringList.get(position);
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(ApplicationPackageName);
        editor.putString("task list", json);
        editor.apply();

And This is the Code which i want to fetch the saved package name.

   SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
       Gson gson = new Gson();
       String json = sharedPreferences.getString("task list", null);
       Type type = new TypeToken<ArrayList<String>>() {}.getType();
       arrayList = gson.fromJson(json, type);

       if (arrayList == null) {
           arrayList = new ArrayList<>();
       }

But I'm getting this error. Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $

Any Help ?

ismail
  • 167
  • 1
  • 8
  • On which of those lines does it crash? – just_user Sep 11 '19 at 07:29
  • refer this link https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Bunny Sep 11 '19 at 07:31
  • @just_user This line is crashing ` arrayList = gson.fromJson(json, type);` – ismail Sep 11 '19 at 07:37
  • I think I just saw the problem: `String ApplicationPackageName = adapter.stringList.get(position);` This is already a string. So what you are storing in sharedpreferences is not a json array. Put a breakpoint or a log message on this line `String json = sharedPreferences.getString("task list", null);` to see the value you get. – just_user Sep 11 '19 at 07:43
  • @just_user I'm getting this "task list". how can i get the package name also ? – ismail Sep 11 '19 at 07:50
  • Try the answer below. Looks right! – just_user Sep 11 '19 at 07:55

1 Answers1

1

On your build,gradle please add

implementation 'com.google.code.gson:gson:2.8.5'

then please check below code.

 package testproject.etisatlat.com.test;

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;

    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;

    import java.lang.reflect.Type;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;

    public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            List<String> test=new ArrayList<>();
            test.add("a");
            test.add("b");
            test.add("c");
            savePackages(test,"SAVE_LIST");
            List<String> savedList=getPackagesList("SAVE_LIST");
        }

        public void savePackages(List<String> packagesList, String key){
            SharedPreferences SharedPreferences= getSharedPreferences("APP_SHARED_PREFS", Activity.MODE_PRIVATE);

            SharedPreferences.Editor editor = SharedPreferences.edit();
            Gson gson = new Gson();
            String json = gson.toJson(packagesList);
            editor.putString(key, json);
            editor.apply();

        }

        public List<String> getPackagesList(String key){
            SharedPreferences SharedPreferences= getSharedPreferences("APP_SHARED_PREFS", Activity.MODE_PRIVATE);
            Gson gson = new Gson();
            String json = SharedPreferences.getString(key, null);
            Type type = new TypeToken<ArrayList<String>>() {}.getType();
            return gson.fromJson(json, type);
        }
    }
Emad Seliem
  • 608
  • 1
  • 4
  • 5