专业:计算机科学与技术班级:1301学号:3138903111姓名:指导教师:完成日期:2016年4月11日实验2基于文件的日程安排【实验目的】掌握首选项方式的存储和读取掌握位于内存上的数据文件的存储和读取掌握位于SD卡上的数据文件的存储和读取【实验环境】Eclipse【实验内容】实现基于文件存储的日程安排应用,数据文件可以保存在机身内存或者SD卡中。通过单击增加图标打开新增加活动界面,通过单击保存按钮将活动内容写入到文件中,并返回前页,点击删除图标,可以删除该活动。(保存或删除须给出提示框)【实验代码及运行结果】packagecom.example.activity;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.List;importandroid.animation.AnimatorSet.Builder;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.DialogInterface;importandroid.content.DialogInterface.OnClickListener;importandroid.content.Intent;importandroid.os.Bundle;importandroid.os.Environment;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsActivity{privateListStringlist;privateListViewlistview;privateMyAdapteradapter;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list=newArrayListString();list=query();listview=(ListView)findViewById(R.id.listview);adapter=newMyAdapter();listview.setAdapter(adapter);}publicListStringquery(){ListStringlist=newArrayListString();Stringstr=;FileInputStreamfis;try{fis=openFileInput(text.txt);byte[]buffer=newbyte[fis.available()];fis.read(buffer);str=newString(buffer);fis.close();}catch(Exceptione){}String[]array=str.split(\n);for(inti=0;iarray.length;i++){Stringst=array[i];list.add(st);}returnlist;}publicvoidaddImage(Viewview){Intentintent=newIntent();intent=newIntent(MainActivity.this,SecondActivity.class);startActivity(intent);}privateclassMyAdapterextendsBaseAdapter{publicintgetCount(){returnlist.size();}publicObjectgetItem(intposition){returnlist.get(position);}publiclonggetItemId(intposition){returnposition;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewview=convertView!=null?convertView:View.inflate(getApplicationContext(),R.layout.list_item,null);TextViewactivity=(TextView)view.findViewById(R.id.activity);finalStringname=list.get(position);activity.setText(name.toString());ImageViewdelete=(ImageView)view.findViewById(R.id.delete);delete.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewarg0){newAlertDialog.Builder(MainActivity.this).setTitle(确定要删除吗?).setPositiveButton(android.R.string.ok,newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){list.remove(name);Stringstr=;notifyDataSetChanged();for(inti=0;ilist.size();i++){str+=list.get(i).toString()+\n;}FileOutputStreamfos;try{fos=openFileOutput(text.txt,MODE_PRIVATE);fos.write(str.getBytes());fos.close();}catch(Exceptione){e.printStackTrace();}}}).setNegativeButton(android.R.string.cancel,newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){dialog.cancel();}}).show();}});returnview;}}}packagecom.example.activity;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.FileWriter;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.List;importandroid.app.Activity;importandroid.app.DatePickerDialog;importandroid.app.TimePickerDialog;importandroid.content.Intent;importandroid.os.Bundle;importandroid.os.Environment;importandroid.view.View;importandroid.widget.Button;importandroid.widget.DatePicker;importandroid.widget.EditText;importandroid.widget.TimePicker;publicclassSecondActivityextendsActivity{privateEditTextdate,start,end,inform;privateintyear,month,day,hour,minute;privateButtonsave;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.additem);date=(EditText)findViewById(R.id.date);start=(EditText)findViewById(R.id.start);end=(EditText)findViewById(R.id.end);inform=(EditText)findViewById(R.id.inform);save=(Button)findViewById(R.id.save);save.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewarg0){Stringstr=date.getText().toString()++start.getText().toString()+-+end.getText().toString()++inform.getText().toString()+\n;FileOutputStreamfos;try{fos=openFileOutput(text.txt,MODE_APPEND);fos.write(str.getBytes());fos.close();}catch(Exceptione){e.printStackTrace();}Intentintent=newIntent();intent=newIntent(SecondActivity.this,MainActivity.class);startActivity(intent);}});}publicvoidchooseDate(Viewview){DatePickerDialogdatepd=newDatePickerDialog(this,newDatePickerDialog.OnDateSetListener(){@OverridepublicvoidonDateSet(DatePickerdp,intyear,intmonth,intday){SecondActivity.this.year=year;SecondActivity.this.month=month;SecondActivity.this.day=day;date.setText(year+-+(month+1)+-+day);}},2016,4,6);datepd.setMessage(请选择日期);datepd.show