내부저장소를 이용하여 저장하고 불러오는 소스를 개발하였습니다.
우선, 소스를 먼저 보여드리겠습니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText an_Check_1, an_Check_2, an_Check_3, an_Check_4, an_Check_5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
an_Check_1 = (EditText) findViewById(R.id.an_check_1);
an_Check_2 = (EditText) findViewById(R.id.an_check_2);
an_Check_3 = (EditText) findViewById(R.id.an_check_3);
an_Check_4 = (EditText) findViewById(R.id.an_check_4);
an_Check_5 = (EditText) findViewById(R.id.an_check_5);
}
public void save(View view){
String an_Check1 = an_Check_1.getText().toString();
String an_Check2 = an_Check_2.getText().toString();
String an_Check3 = an_Check_3.getText().toString();
String an_Check4 = an_Check_4.getText().toString();
String an_Check5 = an_Check_5.getText().toString();
File file = null;
an_Check1 = an_Check1+" ";
an_Check2 = an_Check2+" ";
an_Check3 = an_Check3+" ";
an_Check4 = an_Check4+" ";
an_Check5 = an_Check5+" ";
FileOutputStream fileOutputStream = null;
try {
file = getFilesDir();
fileOutputStream = openFileOutput("anda.txt", Context.MODE_PRIVATE);
fileOutputStream.write(an_Check1.getBytes());
fileOutputStream.write(an_Check2.getBytes());
fileOutputStream.write(an_Check3.getBytes());
fileOutputStream.write(an_Check4.getBytes());
fileOutputStream.write(an_Check5.getBytes());
} catch (IOException e) {
Log.d("anda", e.toString());
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(this, "Saved successfully"+file+"/anda.txt ", Toast.LENGTH_LONG).show();
}
public void next(View view){
Toast.makeText(this, "Data page", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DBActivity.class);
startActivity(intent);
}
}
DBActivity.java
public class DBActivity extends AppCompatActivity {
TextView an_Check_1, an_Check_2, an_Check_3, an_Check_4, an_Check_5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);
an_Check_1 = (TextView)findViewById(R.id.an_txt_1);
an_Check_2 = (TextView)findViewById(R.id.an_txt_2);
an_Check_3 = (TextView)findViewById(R.id.an_txt_3);
an_Check_4 = (TextView)findViewById(R.id.an_txt_4);
an_Check_5 = (TextView)findViewById(R.id.an_txt_5);
}
public void Show(View view){
try {
FileInputStream fileInputStream = openFileInput("anda.txt");
int read = 1;
StringBuffer buffer = new StringBuffer();
while( (read = fileInputStream.read()) != 1 ){
buffer.append((char)read);
}
Log.d(buffer.toString(), "anda");
String m = buffer.toString();
String[] data = m.split(" ");
String an_Check1 = data[0];
String an_Check2 = data[1];
String an_Check3 = data[2];
String an_Check4 = data[3];
String an_Check5 = data[4];
an_Check_1.setText(an_Check1);
an_Check_2.setText(an_Check2);
an_Check_3.setText(an_Check3);
an_Check_4.setText(an_Check4);
an_Check_5.setText(an_Check5);
} catch (FileNotFoundException e) {
Toast.makeText(this, "FileNotFound", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Data found", Toast.LENGTH_LONG).show();
}
public void back(View view){
Toast.makeText(this, "Main Page", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
이런 식으로 데이터를 입력하여 다른 화면에서 저장한 내용을 불러와 보일 수 있도록 코드를 짜보았습니다.
(오류 없는 상태이며 잘 돌아가는 소스입니다..)
저는 여기서 더 나아가 데이터를 하나만 입력하고 저장 후 다음 화면에서도 저장된 갯수만큼 보여지고 싶습니다. 어떤 식으로 수정을 해야할까요?? 도와주십시오!!