我救了我的任务,但我不能检索使用共享preferences的所有任务? [英] I saved my tasks but i can't retrieve all the tasks using shared preferences?
本文介绍了我救了我的任务,但我不能检索使用共享preferences的所有任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我救更多的任务,但在获取任务由一个显示的。我需要显示所有任务。
这是我的code存储任务。
公共静态INT I = 0;task_ok.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){ 字符串prefs_name = NULL;
共享preferences提醒= getApplicationContext()getShared preferences(prefs_name,0);
共享preferences.Editor taskedit = remind.edit();
taskedit.putString(TASKNAME+ I,edit_task.getText()的toString()修剪());
taskedit.putString(taskdate+ I,edit_date.getText()的toString()修剪());
taskedit.putString(tasktime+ I,edit_time.getText()的toString()修剪());
taskedit.apply(); 我++; Toast.makeText(getApplicationContext(),任务中添加,Toast.LENGTH_SHORT).show(); 意图ret_view =新意图(Add.this,TaskActivity.class);
startActivity(ret_view);
}
});
这是code用于检索。
公共静态INT I = 0;
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_view); TextView的电视=(的TextView)findViewById(R.id.textView2);
字符串prefs_name = NULL;
共享preferences提醒= getApplicationContext()getShared preferences(prefs_name,0); remind.getAll();
字符串viewtask = remind.getString(TASKNAME+我,);
字符串viewdate = remind.getString(taskdate+我,);
字符串viewtime = remind.getString(tasktime+我,);
tv.setText(新的StringBuilder()追加(viewtask).append().append(viewdate).append().append(viewtime));
我++;
解决方案
试试这样
字符串TASKNAME =(TASKNAME+ I);
串taskdate =(taskdate+ I);
串tasktime =(tasktime+ I);
taskedit.putString(TASKNAME,edit_task.getText()的toString()修剪());
taskedit.putString(taskdate,edit_date.getText()的toString()修剪());
taskedit.putString(tasktime,edit_time.getText()的toString()修剪());
检索
字符串TASKNAME =(TASKNAME+ I);
串taskdate =(taskdate+ I);
串tasktime =(tasktime+ I);
字符串viewtask = remind.getString(TASKNAME,);
串viewdate = remind.getString(taskdate,);
串viewtime = remind.getString(tasktime,);
,当你从检索每股preference数据做完全相同。
这code为我工作。希望这会帮助你,以及
干杯
I saved more tasks, but while retrieving the tasks are displayed one by one. I need to display all the tasks. This is my code to store the tasks.
public static int i=0;
task_ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String prefs_name = null;
SharedPreferences remind = getApplicationContext().getSharedPreferences(prefs_name,0);
SharedPreferences.Editor taskedit=remind.edit();
taskedit.putString("taskname"+i, edit_task.getText().toString().trim());
taskedit.putString("taskdate"+i, edit_date.getText().toString().trim());
taskedit.putString("tasktime"+i, edit_time.getText().toString().trim());
taskedit.apply();
i++;
Toast.makeText(getApplicationContext(), "Task added", Toast.LENGTH_SHORT).show();
Intent ret_view=new Intent(Add.this,TaskActivity.class);
startActivity(ret_view);
}
});
This is the code for retreiving..
public static int i=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
TextView tv=(TextView)findViewById(R.id.textView2);
String prefs_name = null;
SharedPreferences remind= getApplicationContext().getSharedPreferences(prefs_name,0);
remind.getAll();
String viewtask=remind.getString("taskname"+i, "");
String viewdate=remind.getString("taskdate"+i, "");
String viewtime=remind.getString("tasktime"+i, "");
tv.setText(new StringBuilder().append(viewtask).append(" ").append(viewdate).append(" ").append(viewtime));
i++;
解决方案
try it like this
String taskname = ("taskname"+i);
String taskdate = ("taskdate"+i);
String tasktime = ("tasktime"+i);
taskedit.putString(taskname, edit_task.getText().toString().trim());
taskedit.putString(taskdate, edit_date.getText().toString().trim());
taskedit.putString(tasktime, edit_time.getText().toString().trim());
Retrieving
String taskname = ("taskname"+i);
String taskdate = ("taskdate"+i);
String tasktime = ("tasktime"+i);
String viewtask=remind.getString(taskname, "");
String viewdate=remind.getString(taskdate, "");
String viewtime=remind.getString(tasktime, "");
and do exactly same when you are retrieving data from sharePreference. That code works for me . Hope this will help you as well Cheers
这篇关于我救了我的任务,但我不能检索使用共享preferences的所有任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文