Good night, semuanya apa kabar, semoga anda senantiasa berada dalam lindungan Tuhan. Seperti biasanya saya akan men-share postingan tentang cara membuat penyimpanan data ke memory internal. Pada metoda ini, data disimpan langsung ke sebuah file yang kita beri nama akan tersimpan di SD Card atau Memory internal.

Untuk lebih lengkapnya kita ikuti saja tutorial dibawah ini.
Pada folder res/layout ubah file activity_main.xml menjadi seperti dibawah ini.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = ".MainActivity" > < TextView android:id = "@+id/textView6" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView3" android:layout_below = "@+id/editText3" android:layout_marginTop = "14dp" android:text = "NAMA FILE" /> < EditText android:id = "@+id/editText1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentTop = "true" android:layout_alignRight = "@+id/editText2" android:layout_toRightOf = "@+id/textView2" android:ems = "10" /> < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/editText1" android:layout_alignBottom = "@+id/editText1" android:layout_alignParentLeft = "true" android:text = "NPM" /> < TextView android:id = "@+id/textView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView1" android:layout_below = "@+id/editText1" android:layout_marginTop = "14dp" android:text = "NAMA" /> < EditText android:id = "@+id/editText2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView2" android:layout_alignBottom = "@+id/textView2" android:layout_alignRight = "@+id/editText3" android:layout_toRightOf = "@+id/textView3" android:ems = "10" /> < EditText android:id = "@+id/editText3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView3" android:layout_alignBottom = "@+id/textView3" android:layout_toRightOf = "@+id/textView6" android:ems = "10" /> < TextView android:id = "@+id/textView3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView2" android:layout_below = "@+id/editText2" android:layout_marginTop = "28dp" android:text = "ALAMAT" /> < EditText android:id = "@+id/editText4" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/textView6" android:layout_alignBottom = "@+id/textView6" android:layout_alignLeft = "@+id/editText3" android:layout_alignRight = "@+id/editText3" android:ems = "10" > < requestFocus /> </ EditText > < Button android:id = "@+id/button4" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView6" android:layout_below = "@+id/editText4" android:layout_marginTop = "20dp" android:text = "Baca Int" /> < Button android:id = "@+id/button2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBaseline = "@+id/button4" android:layout_alignBottom = "@+id/button4" android:layout_marginLeft = "21dp" android:layout_toRightOf = "@+id/button4" android:text = "Simpan Int" /> </ RelativeLayout > |
Selanjutnya, ubah file MainActivity.java seperti koding dibawah ini.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package com.android.internal; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.view.View.OnClickListener; public class MainActivity extends Activity { Button simpint, bacint; EditText nim, nama, alamat, namafile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nim = (EditText) findViewById(R.id.editText1); nama = (EditText) findViewById(R.id.editText2); alamat = (EditText) findViewById(R.id.editText3); namafile = (EditText) findViewById(R.id.editText4); simpint = (Button) findViewById(R.id.button2); bacint = (Button) findViewById(R.id.button4); simpint.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String filename = namafile.getText().toString(); String nimm = "\nNIM : " + nim.getText().toString() + "\n"; String namaa = "\nNAMA : " + nama.getText().toString() + "\n"; String alamatt = "\nALAMAT : " + alamat.getText().toString() + "\n"; String bio = ".:: BIODATA ::." + "\n"; FileOutputStream fos; try { fos = openFileOutput(filename, Context.MODE_PRIVATE); fos.write(bio.getBytes()); fos.write(nimm.getBytes()); fos.write(namaa.getBytes()); fos.write(alamatt.getBytes()); fos.close(); Toast.makeText(getApplicationContext(), filename + " disimpan", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); bacint.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String filename = namafile.getText().toString(); StringBuffer stringbuper = new StringBuffer(); try { BufferedReader mYreader = new BufferedReader( new InputStreamReader(openFileInput(filename))); String inputstring; while ((inputstring = mYreader.readLine()) != null) { stringbuper.append(inputstring + "\n"); } } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), stringbuper.toString(), Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |

Jika sudah tersimpan, untuk melihat data tersebut tekan tombol Baca. Selamat mencoba.

No comments:
Post a Comment