我正在创建待办事项列表.现在我的应用程序在ListView中添加和删除数据.但是,当我关闭我的应用程序时,数据将被删除.我想使用SharedPreferences来保存数据onDestroy(),然后当我的应用程序打开时,我想要重新加载数据.
但是,我不知道如何完成这项任务. 任何人都可以帮助我使用共享首选项(也就是我正在寻找代码)保存ListView?
有一个共享首选项的教程我希望在我的应用程序关闭时动态添加多个字符串.然后,当我重新打开它时,数据将出现.我只使用ONE ACTIVITY页面,一切都会在一页上发生.
这是我的代码:
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
// set ListView item listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试开发一个用作购物清单的应用程序,其中用户将文本输入到 an 中EditText,按 a Button,然后将值保存到 a 中List,然后ListView用其值更新 a 。该应用程序运行良好,除非用户退出该应用程序,在这种情况下,用户输入的所有值都不会保存。这是我迄今为止尝试存储List.
public class MainActivity extends ListActivity {
private static final String SHARED_PREFS_NAME = "MY_SHARED_PREF";
ArrayList<String> mylist = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(android.R.id.list);
Button btn = (Button) findViewById(R.id.button);
mylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit …Run Code Online (Sandbox Code Playgroud) 我正在创建一个歌曲播放应用程序.我有一个我在SharedPreferences中保存的Set.我想从中添加和删除文件名,并按照我添加它们的顺序保存它们,例如:
recentsp = getSharedPreferences("recentkey", Context.MODE_PRIVATE);
recent = recentsp.getStringSet("recent", recent);
recentedited = recent;
if (recentedited.contains(string) {
recentedited.remove(string);
Log.i(TAG, "Recent exists, removing song");
SharedPreferences.Editor editor = recentsp.edit();
editor.clear();
editor.putStringSet("recent", recentedited);
editor.commit();
}
recentedited.add(string);
Log.i(TAG, "adding song to recent");
SharedPreferences.Editor editor = recentsp.edit();
editor.clear();
editor.putStringSet("recent", recentedited);
editor.commit();
Run Code Online (Sandbox Code Playgroud)
但是当我在ListView中查看这些问题时会出现问题.我希望它们按照我添加它们的顺序,以便我可以有一个最近播放的部分,但有时它们根本不移动,或者它们可能最终在开头.这似乎是随机的.我错过了什么吗?
编辑:
我通过在初始化步骤中执行此操作来检查以确保SharedPreferences不为空...
编辑:
即使使用LinkedHashSet,我仍然没有得到正确的订购.如果SharedPreferences为null,我只调用它,所以我不确定如何确保我使用的是LinkedHashSet.
recentsp = getSharedPreferences("recentkey", Context.MODE_PRIVATE);
recent = recentsp.getStringSet("recent", null);
if (recent == null) {
recent = new LinkedHashSet<String>();
SharedPreferences.Editor editor = recentsp.edit();
editor.clear();
editor.putStringSet("recent", recent);
editor.commit();
}
Run Code Online (Sandbox Code Playgroud) 在我的项目中,我有一个字符串列表。我想将此列表保存到共享首选项。任何人都可以帮忙吗?
data class select(
@SerializedName("items")
var items: MutableList<String>?=null
)
Run Code Online (Sandbox Code Playgroud) 我正在建立一个家庭替代应用程序.我需要将ArrayList与用户选择要在内部存储器中的启动器上显示的应用程序一起存储.我的意思是关闭应用程序时不得删除该数组.
我非常接近完成应用程序,我不认为我会在Java上工作得更多,我不是程序员所以我只想要最简单的方法来做到这一点.如何在内部存储器中存储和检索ArrayList?
我有一个ArrayList,可以包含1到15个字符串.这些字符串需要保存在我的共享首选项中,现在我知道如何通过数组迭代来获取字符串.我不知道的是,有一种干净的方法可以动态地将字符串添加到我的共享首选项文件中吗?我可以通过创建15个字符串并使用if语句填充共享首选项来缓慢地执行此操作,但我想知道是否有更好的方法.
谢谢.
我有以下示例代码.应用程序第一次安装成功.但是,它会在重新安装时抛出错误.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinkedHashSet<String> planets = new LinkedHashSet<String>();
planets.add("Earth");
SharedPreferences prefs = getPreferences(0);
prefs.edit().putStringSet("planets", planets).commit();
prefs = getPreferences(0);
planets = (LinkedHashSet<String>) prefs.getStringSet("planets", new LinkedHashSet<String>());
}
}
Run Code Online (Sandbox Code Playgroud)
我已经粘贴了重新安装以下应用程序时产生的错误.
Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.LinkedHashSet at com.example.test.MainActivity.onCreate(MainActivity.java:12)
Run Code Online (Sandbox Code Playgroud)
我想了解为什么保存LinkedHashSet不能被归还给LinkedHashSet.为什么它会HashSet被Android自动转换为?
我使用共享首选项在我的闹钟中创建了多个闹钟,现在我希望闹钟应该在一周中的特定一天响起,为此我想要一个可以保存闹钟响铃日期的数组。谁能告诉如何将数组放入共享首选项以及如何再次检索它..?
SharedPreferences set=getSharedPreferences(MYPR, 0);
SharedPreferences.Editor ed=set.edit();
ed.putInt("Ahh", Ahh);
ed.putInt("Amm",Amm);
ed.putBoolean("en", true);
ed.commit();
Run Code Online (Sandbox Code Playgroud)
现在我将闹钟时间及其属性放在共享首选项中。要添加什么来放置数组(星期几)也..?
我在Android应用程序中实现了警报.报警工作正常.Toast消息是可见的.
现在我想向用户发出警报框通知.
这是来自ReceiverActivityClass的代码.我试过了
public class ReceiverActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Code....
new AlertDialog.Builder(context)
.setTitle("Alert Box")
.setMessage("Msg for User")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
// some coding...
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
}).create().show();
}
Run Code Online (Sandbox Code Playgroud)
}
android broadcastreceiver android-alarms android-alertdialog