Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5 #881 第五次实验 #2228

Merged
merged 2 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,67 @@
package edu.hzuapps.androidlabs.soft1614080902117;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.app.Activity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Soft1614080902117Activity extends Activity {
String[] adapterData;

Button button = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft1614080902117);

button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
final ListView listView = (ListView)findViewById(R.id.listView);
adapterData = new String[] {load()};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
( Soft1614080902117Activity.this, android.R.layout.simple_list_item_1, adapterData);
listView.setAdapter(arrayAdapter);
final Activity thisActivity = this;
Button btnHome = (Button)findViewById(R.id.button);
btnHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Soft1614080902117Activity.this,Soft1614080902117Activity1.class);
startActivity(intent);
Intent intent = new Intent(thisActivity,Soft1614080902117Activity1.class);
thisActivity.startActivity(intent);
}
});
}
public String load(){ //读取文件
FileInputStream in =null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line=reader.readLine())!= null){
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,67 @@
package edu.hzuapps.androidlabs.soft1614080902117;
import android.content.Intent;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.String;
import android.widget.EditText;


public class Soft1614080902117Activity1 extends Activity {

Button button = null;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft16140809021171);
final Activity thisActivity = this;
Button btnBack = (Button)findViewById(R.id.button3);
Button btnJoin= (Button)findViewById(R.id.button4);
editText = (EditText)findViewById(R.id.editText);

button = (Button)findViewById(R.id.button4);
button.setOnClickListener(new OnClickListener() {
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(thisActivity,Soft1614080902117Activity.class);
thisActivity.startActivity(intent);
}
});
btnJoin.setOnClickListener(new View.OnClickListener() { //添加保存文件
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Soft1614080902117Activity1.this,Soft1614080902117Activity.class);
startActivity(intent);
String inputText = editText.getText().toString();
save(inputText);
Intent intent = new Intent(thisActivity,Soft1614080902117Activity.class);
thisActivity.startActivity(intent);
}
});
}
}

public void save(String inputText){
FileOutputStream out;
BufferedWriter writer = null;
try {
out = openFileOutput("data", MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}
38 changes: 38 additions & 0 deletions soft1614080902117/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 第五次实验

## 一、实验目标

掌握在Android App中存储数据

## 二、实验内容

使用文件存储

1.将应用产生的数据保存到文件存储中;

2.说明使用的文件存储方式:内部 or 外部;

3.将运行结果截图。

## 三、实验步骤
1.选用内容部存储的方式来存取;

2.在主界面Soft1614080902117Activity中添加文件读取函数public String load();
将读取的文件内容转换成字符串赋给字符串数字adapterData,再添加到listview的item中,
每一次返回主界面都会读取一次文件;

3.在编辑界面Soft1614080902117Activity1中添加文件存储函数public void save
(String inputText);主要是将editText中输入的文本转换为字符串赋给inputText,并
传入save函数中,进行文件保存;通过点击√(保存)按钮进行保存并跳转回主界面,点击×则
放弃修改且返回主界面;

## 四、实验截图
编辑
![image](https://github.com/1614080902117/android-labs-2018/blob/master/soft1614080902117/shiyan5.1.png)
保存
![image](https://github.com/1614080902117/android-labs-2018/blob/master/soft1614080902117/shiyan5.2.png)

## 五、实验体会
本次实验是了解使用Android App中存储数据的方式,在实验过程中确实是不太懂得这些存储方式的具体
是如何实现的,只能通过参考已经实现的代码进行修改来完成自己的课题,本来想用数据库实现的,但是
过程中出现了很多错误最后只能选用内部存储暂时实现存储,数据库的存储还在自己摸索当中。
Binary file added soft1614080902117/shiyan5.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902117/shiyan5.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.