[英]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.如果我在完成后打开新的倒数计时器它会将新文本添加到列表视图但它只保存最后一个文本如何将所有文本保存在 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.如果要存储列表或大量数据,请使用带有 Room 的本地数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.