0

I have been able to save one entry to shared preferences and for it to display in a list view on another view but I am wanting to add multiple entries and them to display in the listview too. I thought I had the correct code but it doesn't see mto have changed anything. My intent is a favourites list, I take the entry data from one view and display it in another view.

SingleView Activity:

SharedPreferences.Editor fd;
SharedPreferences FeedPref;

private ArrayList<String> addArray = new ArrayList<>();

    txt = (TextView) findViewById(R.id.name);

    add = (Button) findViewById(R.id.btnAdd);
    FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    fd = FeedPref.edit();


    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String message = txt.getText().toString();

            if (addArray.contains(message)) {
                Toast.makeText((getBaseContext()), "Plant Already Added", Toast.LENGTH_LONG).show();
            } else {
                addArray.add(message);

                FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                fd = FeedPref.edit();
                fd.putInt("array_size", addArray.size());

                for (int i = 0; i < addArray.size(); i++) {
                    fd.putString("Status_" + i, addArray.get(i));
                }
                   fd.commit();
                    Toast.makeText((getBaseContext()), "Plant Added", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

mygarden activity:

public class mygardenMain extends Activity {
//String[] presidents;
ListView listView;
//ArrayAdapter<String> adapter;
SharedPreferences FeedPref;
SharedPreferences.Editor fd;
//private ArrayList<String> addArray;
//public static final String PREFS = "examplePrefs";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mygarden_list);
    listView = (ListView) findViewById(R.id.mygardenlist);
    //addArray = new ArrayList<>();

    FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    int size = FeedPref.getInt("array_size", 0);

    for (int i = 0; i < size; i++) {
        String mess = FeedPref.getString("Status_" + i, null);
        String[] values = new String[]{mess};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);

    }
}
user4682589
  • 49
  • 1
  • 2
  • 13
  • fd.commit(); was to be called as soon as you put the string. And this is not ideal way to do it. – rohitanand Feb 14 '17 at 10:46
  • why not save an set instead, I think it will be clearer, take a look in this post: http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences , than on onclick event, you should get the set from shared preference, add the new item and save it. – fdam Feb 14 '17 at 10:47
  • That is the thread I used to code from where I got to today. – user4682589 Feb 14 '17 at 10:56
  • is there a way to declare an arraylist without creating a new one with each click? – user4682589 Feb 14 '17 at 11:36

2 Answers2

1

Set abc = new HashSet<>();

abc.add("john");

abc.add("test");

abc.add("again");

    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putStringSet("key",abc);

    editor.commit();
wVV
  • 63
  • 1
  • 10
0

SingleView Activity:

    SharedPreferences.Editor fd;
    SharedPreferences FeedPref;

    private ArrayList<String> addArray = new ArrayList<>();

        txt = (TextView) findViewById(R.id.name);

        add = (Button) findViewById(R.id.btnAdd);
        FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        fd = FeedPref.edit();


        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String message = txt.getText().toString();

                if (addArray.contains(message)) {
                    Toast.makeText((getBaseContext()), "Plant Already Added", Toast.LENGTH_LONG).show();
                } else {

                   FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    fd = FeedPref.edit();

                    Gson gson = new Gson();
                    String jsonText = Prefs.getString("key", "");
                    if(!jsonText.equals(""))
                    {
                        String[] text = gson.fromJson(jsonText, String[].class);  //EDIT: gso to gson
                        if(text.length>0)
                        {
                            //addArray = Arrays.asList(text);
                            //addArray = new ArrayList(addArray);
                            List<String> addArrayNew = Arrays.asList(text);
                            addArray = new ArrayList(addArrayNew);

                        }
                    }
                    addArray.add(message);

                    gson = new Gson();
                    jsonText = gson.toJson(addArray );
                    prefsEditor.putString("key", jsonText);
                    prefsEditor.commit();
                }
            });
        }

mygarden activity:

    public class mygardenMain extends Activity {
    //String[] presidents;
    ListView listView;
    //ArrayAdapter<String> adapter;
    SharedPreferences FeedPref;
    SharedPreferences.Editor fd;
    //private ArrayList<String> addArray;
    //public static final String PREFS = "examplePrefs";
    String jsonText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mygarden_list);
        listView = (ListView) findViewById(R.id.mygardenlist);
        //addArray = new ArrayList<>();

        FeedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        int size = FeedPref.getInt("array_size", 0);

         Gson gson = new Gson();
         jsonText = FeedPref.getString("key", "");
         if(!jsonText.equals(""))
         {
           String[] values= gson.fromJson(jsonText, String[].class);
           ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
           listView.setAdapter(adapter);
        }

    }
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • Im getting an error with the .addAll (data); How does it know what string to input? its showing data in red. – user4682589 Feb 14 '17 at 10:52
  • also getting a needs to be declared final error on the jsonText parts – user4682589 Feb 14 '17 at 10:52
  • You already have data in addArray, so remove line addArray .addAll(data); And declare String jsonText above on create and then use in onclick – Komal12 Feb 14 '17 at 11:03
  • In SingleView Activity check size of addArray before adding data into SharedPreferences and in mygarden activity check the size of String[] values – Komal12 Feb 14 '17 at 11:19
  • New code works but is still only displaying 1 input. – user4682589 Feb 14 '17 at 11:25
  • if it helps, I have a listview which is populated by a parse database, this lists 50 different plants, when a plant is selected it goes into a detail view displaying more info about that plant, at the bottom there is a 'add to my garden' button. This would mean the view is reloaded each time a plant is selected, therefor I think its creating a new array each time?? – user4682589 Feb 14 '17 at 11:29
  • updated SingleView Activity class. 1st fetch all data from SharedPreferences then add old data and new message in SharedPreferences – Komal12 Feb 15 '17 at 02:49
  • I have a new error now: Attempt to get length of null array. – user4682589 Feb 15 '17 at 12:44
  • the main error being on the line: addArray = Arrays.asList(text); incompatible types – user4682589 Feb 15 '17 at 12:51
  • Check updated code . Add List addArrayNew = Arrays.asList(text); addArray = new ArrayList(addArrayNew); – Komal12 Feb 16 '17 at 02:54
  • still get the error: Attempt to get length of null array – user4682589 Feb 16 '17 at 09:22