我有一个名为tv的文本视图,它从网页中获取内容。我想在按钮点击时存储该文本视图的内容,但我不希望有一天的内容覆盖前一天的内容(例如今天tv = ok和明天tv = ok1,我想要存储好的和ok1不仅ok1) 当然,对于共享首选项,我可以存储该文本,但我不想覆盖前一天的文本
TextView tv =(TextView)findViewById(R.id.oks); tv.setText(结果[1]的ToString());
//in pass there is the text of tv(in main activity)
SharedPreferences ok = getSharedPreferences("pass", 0);
String str = ""+ok.getString("textvalue","");
SharedPreferences.Editor editor = ok.edit();
editor.putString("textvalue",str);
editor.apply();
TextView textView=(TextView) findViewById(R.id.preferiti);
textView.setText(ok.getString("textvalue",""));
答案 0 :(得分:0)
这是你的textview
//this are two fields which you want to append and getting somehow from API
String todayTv="ok" , tomorrowTv="ok1";
TextView tv = (TextView) findViewById(R.id.oks);
让我们在textView
上设置今天的值 tv.setText(todayTv);
现在要存储此值
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //getting value from textview
edit.apply();
保存!假设你现在有了明天你要用你以前的结果显示的那个,就像这样ok ok1
tv.setText(sharedPref.getString("textvalue","")+" "+tomorrowTv); //Concat strings and set text
旧值 - sharedPref.getString(" textvalue","")
新价值 - 明天电视
现在再次使用上面的代码保存它以备将来
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //It will give you final string containg old and new value into one string
edit.apply();
希望它会让你了解它。
阅读更多:https://developer.android.com/training/basics/data-storage/shared-preferences.html https://developer.android.com/reference/android/widget/TextView.html