我保存了更多任务,但是在检索任务时会一一显示。我需要显示所有任务。这是我存储任务的代码。
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);
}
});
这是检索的代码..
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++;