0

我保存了更多任务,但是在检索任务时会一一显示。我需要显示所有任务。这是我存储任务的代码。

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++;
4

1 回答 1

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 中检索数据时,执行完全相同的操作。该代码对我有用。希望这对你也有帮助 干杯

于 2012-12-07T18:14:32.257 回答