1

I have a listview in which I want to store specific data everytime the user clicks on the add button. However until now I have had little succes with the implementation. I know that it has to be done by using sharedpreferences and have tried to implement the saving status of sharedpreferences and the restoring status, but still my listview only adds the element that is currently clicked.

    package com.example.daniel.pset3_daniel_jacob;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class listview extends AppCompatActivity {

    ArrayList<String> dataa;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        onRestoredPreferences();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        Bundle extras = getIntent().getExtras();
        String dataExtra = extras.getString("key");
        dataa = new ArrayList<String>();
        dataa.add(dataExtra);
        ListView listView = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
        assert listView != null;
        listView.setAdapter(adapter);
        Savedpreferences(dataa);


    }

    public void Savedpreferences(List<String> dataa) {


        SharedPreferences preferences = this.getSharedPreferences("object", 0);
        SharedPreferences.Editor editor = preferences.edit();
        List<String> arraylist = new ArrayList<String>(dataa);
        Set<String> newset = new HashSet<String>(arraylist);
        editor.putStringSet("stringset", newset);
        editor.commit();
    }


    public void onRestoredPreferences()
    {
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
        Set<String> getdataback = preferences.getStringSet("stringset", null);
        List <String> arraylist = new ArrayList<String>(getdataback);
    }

}

I hope someone can help me to tell me what i am doing wrong

1 Answers1

0

Try the below code and let me know if any issues.

public class ListviewActivity extends AppCompatActivity {

        ArrayList<String> dataa;

        List <String> arraylist = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            onRestoredPreferences();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
            Bundle extras = getIntent().getExtras();
            String dataExtra = extras.getString("key");
            dataa = new ArrayList<String>();
            dataa.addAll(arraylist);
            dataa.add(dataExtra);
            ListView listView = (ListView) findViewById(R.id.listview);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
            assert listView != null;
            listView.setAdapter(adapter);
            Savedpreferences(dataa);


        }

        public void Savedpreferences(List<String> dataa) {


            SharedPreferences preferences = this.getSharedPreferences("object", 0);
            SharedPreferences.Editor editor = preferences.edit();
            List<String> arraylist = new ArrayList<String>(dataa);
            Set<String> newset = new HashSet<String>(arraylist);
            editor.putStringSet("stringset", newset);
            editor.commit();
        }


        public void onRestoredPreferences()
        {
            SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
            Set<String> getdataback = preferences.getStringSet("stringset", null);
            arraylist.clear();
            arraylist.addAll(getdataback);
        }

    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
RaghavPai
  • 652
  • 9
  • 14
  • You do know that `onRestoreInstanceState` and `onSaveInstanceState` are available methods for this exact purpose, right? – OneCricketeer Mar 04 '17 at 18:50
  • Yes i know but i dint want to complicate the answer if he is not aware of it. – RaghavPai Mar 04 '17 at 18:51
  • I used his own logic to fix his issue – RaghavPai Mar 04 '17 at 18:52
  • I see... Though, using a `Set` would remove duplicates, so probably not a good idea – OneCricketeer Mar 04 '17 at 18:59
  • hey guys, it is working now... thank you so much. I guess I missed the addall function. Did not see it in the tutorials. I will set your answer as solved answer raghav and thank you cricket_007 for telling me that I could have solved it better with onrestoreinstancestate –  Mar 04 '17 at 19:17