简体   繁体   中英

How can I save my listView with Shared Preferences in Android Studio

I have edittext, countdowntimer, listview, shared preferences on my project. My app can work. my countdown timer on finish I add text my listview. and I save this with shared preferences. And if I open new countdown timer after finish It add new text to listview but It save only last text How can ı save all text in ListView.

 public class pomodoro extends AppCompatActivity {
        Button baslat,backhome,bitir;
            EditText edittextcalisma,edittextmola;
            CountDownTimer calisma,mola;
            ArrayList<String> list = new ArrayList<String>();
            ArrayAdapter arrayAdapter;
            ListView listView;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_pomodoro);
                 LoadPreferences();
              
                listView=(ListView)findViewById(R.id.listv);
                arrayAdapter = new ArrayAdapter<String>(
                        this,R.layout.list_view,R.id.textitem, list);
                listView.setAdapter(arrayAdapter);
                bitir=findViewById(R.id.bitirbutton);
                baslat = findViewById(R.id.baslatbutton);
                edittextcalisma = findViewById(R.id.edittextcalisma);
                edittextmola = findViewById(R.id.edittextmola);
               
                baslat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
        
                        closeKeyboard();
        
                        final int molapo = Integer.valueOf(edittextmola.getText().toString());
                        final int calismapo = Integer.valueOf(edittextcalisma.getText().toString());
        
                       
 CountDownTimer bekle = new CountDownTimer(5000, 1000) {
                                                @Override
                                                public void onTick(long millis) {
        
        
                                                }
                                                @Override
                                                public void onFinish() {
                                                    
                                                    Calendar c = Calendar.getInstance();
                                                    SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMMM-yyyy HH:mm");
                                                    String datetime = dateformat.format(c.getTime());
        
        
        
                                                    list.add("Çalışma Süresi : " + calismapo +"  dk  "+"\n"+  "Mola Süresi : " + molapo+"  dk  " +"\n" + datetime);
                                                    arrayAdapter.notifyDataSetChanged();                                                                                                          SavePreferences("LISTS", task);
        
        
                                                }
                                            }.start();
                                        }
                                    }.start();
                                }
                            }.start();
                        }
                    }
                });
            }                                                                                                                                                                                                                  protected void SavePreferences(String key, String value) {
         
            SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = data.edit();
            editor.putString(key, value);
            editor.commit();
    
    
        }
    
        protected void LoadPreferences(){
            SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
            String dataSet = data.getString("LISTS", "");
    
            list.add(dataSet);
            arrayAdapter.notifyDataSetChanged();
    
        }
        }

Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device. If you want to Store a list or large amount of data use local database with Room.

Learn More About Room persistence Library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM