아래에 있는 소스처럼 데이터를 입력하여 다른 화면에서 저장한 내용을 불러와 보일 수 있도록 코드를 짜보았습니다.
(오류 없는 상태이며 잘 돌아가는 소스입니다..)
저는 여기서 데이터를 하나씩만 입력해도 저장 후 다음 화면에서도 저장된 갯수만큼 보여지고 싶습니다. 어떤 식으로 수정을 해야할까요?? 제발 도와주십시오!!
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText an_Check_1, an_Check_2, an_Check_3, an_Check_4, an_Check_5;
//public static final int READ_BLOCK_SIZE = 100;
@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) {
e.printStackTrace();
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(this, "Saved successfully", 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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);
}
public void show(View view){
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);
}