我保存了更多任务,但是逐个显示检索任务。我需要显示所有任务。 这是我存储任务的代码。
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);
}
});
这是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++;
答案 0 :(得分:0)
尝试这样
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());
检索
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, "");
并在从sharePreference检索数据时执行完全相同的操作。 该代码适用于我。希望这也会对你有所帮助 干杯