第三次实验基于文件的日程安排
一. 实验目的及实验环境
1. 实验目的
1)掌握首选项方式的存储和读取
2)掌握位于内存上的数据文件的存储和读取
3)掌握位于SD卡上的数据文件的存储和读取
2.实验环境
系统开发平 Android Studio
系统开发平台:Android
运行平台:Windows XP及以上
运行环境: Framework SDK
二. 实验教材、组织方式、实验内容
1.实验教材: Andorid开发与应用
2.组织方式:个人独立完成
2.实验内容:
实现基于文件存储的日程安排应用,数据文件可以保存在机身内存或者SD 卡中。通过单击增加图标打开新增加活动界面,通过单击保存按钮将活动内容写入到文件中,并返回前页,点击删除图标,可以删除该活动。(保存或删除须给出提示框)
三.方案设计
写了一个三个界面,一个是已经安排的活动的界面,一个是新增加活动的界面,还有一个是浏览界面,然后增加活动的时候调用了日期选择框,对日期进行选择,然后对时间进行选择,选择之后,输入记录,然后写入文件存储起来,下次打开app可以查看之前记录的内容。
四.运行结果五.总结
通过这次实验掌握了首选项方式的存储和读取,掌握了位于内存上的数据文件的存储和读取以及位于SD卡上的数据文件的存储和读取
六.附录:源代码
import
import class MainActivity extends Activity {
private List
private ListView listview;
private MyAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView list=new ArrayList
list=query();
listview=(ListView)findViewById adapter=new MyAdapter();
(adapter);
}
public List
{
L ist
S tring str="";
F ileInputStream fis;
t ry{
fis=openFileInput("");
byte[] buffer=new byte[()];
(buffer);
str=new String(buffer);
();
}catch(Exception e)
{}
S tring[] array=("\n");
f or(int i=0;i<;i++)
{
String st=array[i];
(st);
}
r eturn list;
}
public void addImage(View view)
{
I ntent intent = new Intent();
intent = new Intent,;
startActivity(intent);
}
private class MyAdapter extends BaseAdapter
{
public int getCount() {
return ();
}
public Object getItem(int position) {
return (position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view=convertView!=null?convertView:(getApplicationContext(), null);
TextView activity=(TextView) final String name=(position);
());
ImageView delete=(ImageView) (new () {
public void onClick(View arg0) {
new
.setTitle("确定要删除吗?")
.setPositiveButton new () {
public void onClick(DialogInterface dialog, int which) {
(name);
String str="";
notifyDataSetChanged();
for(int i=0;i<();i++)
{
str+=(i).toString()+"\n";
}
FileOutputStream fos;
try{
fos=openFileOutput("",MODE_PRIVATE);
());
();
}catch(Exception e)
{
();
}
}
})
.setNegativeButton new () {
public void onClick(DialogInterface dialog, int which) {
();
}
})
.show();
}
});
return view;
}
}
}
android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbarSize="1dip"> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> android:id="@+id/itemTaskLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/item_task_text" android:textSize="22sp"/> android:id="@+id/itemTaskEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="left" android:hint="@string/addItemHint" android:lines="6"/> android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider"/> android:id="@+id/itemTimeLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/item_time_text" android:textSize="22sp"/> style=" ?android:attr/buttonBarStyle " android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="0dip"> android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider"/> android:id="@+id/alertLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dip"> android:id="@+id/itemAlertLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/item_alert_text" android:textSize="22sp"/> android:id="@+id/itemAlertTime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/itemAlertLabel" android:textSize="14sp"/> android:id="@+id/itemAlertImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@string/item_duplicate_contentDescription" android:src="@drawable/add"/> android:id="@+id/alertTableLayout" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_marginLeft="5dip" android:layout_weight="1" android:padding="5dip" android:stretchColumns="0,1"> android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider"/> android:id="@+id/duplicateLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dip"> android:id="@+id/itemDuplicateLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/item_duplicate_text" android:textSize="22sp"/> android:id="@+id/itemDuplicate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/itemDuplicateLabel" android:textSize="14sp"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@string/item_duplicate_contentDescription" android:src="@drawable/arrow_right"/> android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider"/> android:id="@+id/priorityLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dip"> android:id="@+id/itemPriorityLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/item_priority_text" android:textSize="22sp"/> android:id="@+id/itemPriority" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/itemPriorityLabel" android:textSize="14sp"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@string/item_duplicate_contentDescription" android:src="@drawable/arrow_right"/> android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider"/>