0

我製作了一個android程序,我們可以從編輯文本中設置id等,然後將該值保存到sdcard中的.txt文件中。我已經可以做到這一點(在SD卡中創建一個.txt文件,現在我試圖在應用程序的其他活動中讀取該.txt文件,並在該活動開始/開始時將該值插入編輯文本中。我需要所以我可以在我的應用程序中設置例如「id」一次,然後每次用戶輸入一個表單條目數據,之前設置的一些數據例如「id」已經插入到條目活動中的編輯文本中。讀取SD卡中的.txt文件並插入以編輯文本

這是我從編輯文本插入值的代碼,並在SD卡.txt文件:

public class Setting extends Activity implements OnClickListener { 
    Button submit2; 
    //private String filename = "AsriConfig.txt"; 
    //private String filepath = "MyFileStorage"; 
    //File myInternalFile; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.setting); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); 


     //ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext()); 
     //File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE); 
     //myInternalFile = new File(directory, filename); 

    // Set up click listeners for all the buttons 
     submit2=(Button)findViewById(R.id.buttom_submit2); 
     submit2.setOnClickListener(this); 

    } 


    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     EditText agen2 = (EditText) findViewById(R.id.agen2); 

     switch (v.getId()) { 
     case R.id.buttom_submit2 : 
      try { 
       File myFile = new File("/sdcard/myconfig.txt"); 
       myFile.createNewFile(); 
       FileOutputStream fOut = new FileOutputStream(myFile); 
       OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
       myOutWriter.append(agen2.getText()); 
       myOutWriter.close(); 
       fOut.close(); 
       Toast.makeText(this, "Data telah tersimpan..",Toast.LENGTH_SHORT).show(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      agen2.setText(""); 

      break; 
      } 
    } 

,然後這是我在其他活動代碼從SD卡txt文件的讀取值與自動插入編輯文本,其中活動開始:

public class CreateData extends Activity implements OnClickListener{ 

    //inisilisasi elemen-elemen pada layout 

    private String array_spinner[]; 
    private String array_spinner1[]; 

    private Button buttonSubmit; 
    private EditText edAgen; 
    private EditText edNo_kk; 
    private EditText edNo_blanko; 
    private EditText edNama; 
    private EditText edAlamat; 
    private EditText edJml_tertanggung; 
    private Spinner edPilihan; 
    private EditText edTelepon; 
    private EditText edTertanggung1; 
    private Spinner edStatus1; 
    private EditText edTertanggung2; 
    private Spinner edStatus2; 
    private EditText edTertanggung3; 
    private Spinner edStatus3; 
    private EditText edTertanggung4; 
    private Spinner edStatus4; 
    private EditText edTertanggung5; 
    private Spinner edStatus5; 
    private EditText edTertanggung6; 
    private Spinner edStatus6; 

    private EditText edTertanggung7; 
    private Spinner edStatus7; 
    private EditText edTertanggung8; 
    private Spinner edStatus8; 
    private EditText edTertanggung9; 
    private Spinner edStatus9; 
    private EditText edTertanggung10; 
    private Spinner edStatus10; 

    //inisialisasi kontroller/Data Source 
    private DBDataSource dataSource; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.create_data); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); 

     //File file = new File("/sdcard/myconfig.txt"); 

     String ret = ""; 

     try { 
      InputStream inputStream = openFileInput("myconfig.txt"); 
      if (inputStream != null) { 
       InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
       BufferedReader br = new BufferedReader(inputStreamReader); 
       String line = ""; 
       StringBuilder text = new StringBuilder(); 

      while ((line = br.readLine()) != null) { 
       text.append(line); 
      } 
      inputStream.close(); 
      ret = text.toString(); 
      } 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 

     edAgen.setText(ret); 

     buttonSubmit = (Button) findViewById(R.id.buttom_submit); 
     buttonSubmit.setOnClickListener(this); 
     edAgen = (EditText) findViewById(R.id.agen); 
     edNo_kk = (EditText) findViewById(R.id.no_kk); 
     edNo_blanko = (EditText) findViewById(R.id.no_blanko); 
     edJml_tertanggung = (EditText) findViewById(R.id.jml_tertanggung); 
     edNama = (EditText) findViewById(R.id.nama); 
     edAlamat = (EditText) findViewById(R.id.alamat); 

     array_spinner=new String[10]; 
     array_spinner[0]="A1"; 
     array_spinner[1]="A2"; 
     array_spinner[2]="A3"; 
     array_spinner[3]="A4"; 
     array_spinner[4]="A5"; 
     array_spinner[5]="B1"; 
     array_spinner[6]="B2"; 
     array_spinner[7]="B3"; 
     array_spinner[8]="B4"; 
     array_spinner[9]="B5"; 
     edPilihan = (Spinner) findViewById(R.id.pilihan_paket); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner); 
     edPilihan.setAdapter(adapter); 

     array_spinner1=new String[4]; 
     array_spinner1[0]=" "; 
     array_spinner1[1]="SUAMI"; 
     array_spinner1[2]="ISTRI"; 
     array_spinner1[3]="ANAK"; 

     edStatus1 = (Spinner) findViewById(R.id.status1); 
     ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus1.setAdapter(adapter1); 

     edStatus2 = (Spinner) findViewById(R.id.status2); 
     ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus2.setAdapter(adapter2); 

     edStatus3 = (Spinner) findViewById(R.id.status3); 
     ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus3.setAdapter(adapter3); 

     edStatus4 = (Spinner) findViewById(R.id.status4); 
     ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus4.setAdapter(adapter4); 

     edStatus5 = (Spinner) findViewById(R.id.status5); 
     ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus5.setAdapter(adapter5); 

     edStatus6 = (Spinner) findViewById(R.id.status6); 
     ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus6.setAdapter(adapter6); 

     edStatus7 = (Spinner) findViewById(R.id.status7); 
     ArrayAdapter<String> adapter7 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus7.setAdapter(adapter7); 

     edStatus8 = (Spinner) findViewById(R.id.status8); 
     ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus8.setAdapter(adapter8); 

     edStatus9 = (Spinner) findViewById(R.id.status9); 
     ArrayAdapter<String> adapter9 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus9.setAdapter(adapter9); 

     edStatus10 = (Spinner) findViewById(R.id.status10); 
     ArrayAdapter<String> adapter10 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner1); 
     edStatus10.setAdapter(adapter10); 

     edTelepon = (EditText) findViewById(R.id.telepon); 
     edTertanggung1 = (EditText) findViewById(R.id.tertanggung1); 

     edTertanggung2 = (EditText) findViewById(R.id.tertanggung2); 

     edTertanggung3 = (EditText) findViewById(R.id.tertanggung3); 

     edTertanggung4 = (EditText) findViewById(R.id.tertanggung4); 

     edTertanggung5 = (EditText) findViewById(R.id.tertanggung5); 

     edTertanggung6 = (EditText) findViewById(R.id.tertanggung6); 

     edTertanggung7 = (EditText) findViewById(R.id.tertanggung7); 
     edTertanggung8 = (EditText) findViewById(R.id.tertanggung8); 
     edTertanggung9 = (EditText) findViewById(R.id.tertanggung9); 
     edTertanggung10 = (EditText) findViewById(R.id.tertanggung10); 

     // instanstiasi kelas DBDataSource 
     dataSource = new DBDataSource(this); 

     //membuat sambungan baru ke database 
     dataSource.open(); 
    } 

    //KETIKA Tombol Submit Diklik 
    @Override 
    public void onClick(View v) { 
     // Inisialisasi data barang 
     String agen = null; 
     String no_kk = null; 
     String no_blanko = null; 
     String nama = null; 
     String alamat = null; 
     String jml_tertanggung = null; 
     String pilihan = edPilihan.getSelectedItem().toString(); 
     String telepon = null; 
     String tertanggung1 = null; 
     String status1 = edStatus1.getSelectedItem().toString(); 
     String tertanggung2 = null; 
     String status2 = edStatus2.getSelectedItem().toString(); 
     String tertanggung3 = null; 
     String status3 = edStatus3.getSelectedItem().toString(); 
     String tertanggung4 = null; 
     String status4 = edStatus4.getSelectedItem().toString(); 
     String tertanggung5 = null; 
     String status5 = edStatus5.getSelectedItem().toString(); 
     String tertanggung6 = null; 
     String status6 = edStatus6.getSelectedItem().toString(); 

     String tertanggung7 = null; 
     String status7 = edStatus7.getSelectedItem().toString(); 
     String tertanggung8 = null; 
     String status8 = edStatus8.getSelectedItem().toString(); 
     String tertanggung9 = null; 
     String status9 = edStatus9.getSelectedItem().toString(); 
     String tertanggung10 = null; 
     String status10 = edStatus10.getSelectedItem().toString(); 
     @SuppressWarnings("unused") 


     //inisialisasi barang baru (masih kosong) 
     Barang barang = null; 
     if(edAgen.getText()!=null && edNo_kk.getText()!=null && edNo_blanko.getText()!=null && edNama.getText()!=null && edAlamat.getText()!=null && edJml_tertanggung.getText()!=null && //edPilihan.getContext()!=null && 
       edTelepon.getText()!=null && 
       edTertanggung1.getText()!=null && //edStatus1.getText()!=null && 
       edTertanggung2.getText()!=null && //edStatus2.getText()!=null && 
       edTertanggung3.getText()!=null && //edStatus3.getText()!=null && 
       edTertanggung4.getText()!=null && //edStatus4.getText()!=null && 
       edTertanggung5.getText()!=null && //edStatus5.getText()!=null && 
       edTertanggung6.getText()!=null && 
       edTertanggung7.getText()!=null && 
       edTertanggung8.getText()!=null && 
       edTertanggung9.getText()!=null && 
       edTertanggung10.getText()!=null) //edStatus6.getText()!=null) 
     { 
      /* jika field nama, merk, dan harga tidak kosong 
      * maka masukkan ke dalam data barang*/ 
      agen = edAgen.getText().toString(); 
      no_kk = edNo_kk.getText().toString(); 
      no_blanko = edNo_blanko.getText().toString(); 
      nama = edNama.getText().toString(); 
      alamat = edAlamat.getText().toString(); 
      jml_tertanggung = edJml_tertanggung.getText().toString(); 
      //pilihan = edPilihan.getContext().toString(); 
      telepon = edTelepon.getText().toString(); 
      tertanggung1 = edTertanggung1.getText().toString(); 
      //status1 = edStatus1.getText().toString(); 
      tertanggung2 = edTertanggung2.getText().toString(); 
      //status2 = edStatus2.getText().toString(); 
      tertanggung3 = edTertanggung3.getText().toString(); 
      //status3 = edStatus3.getText().toString(); 
      tertanggung4 = edTertanggung4.getText().toString(); 
      //status4 = edStatus4.getText().toString(); 
      tertanggung5 = edTertanggung5.getText().toString(); 
      //status5 = edStatus5.getText().toString(); 
      tertanggung6 = edTertanggung6.getText().toString(); 
      //status6 = edStatus6.getText().toString(); 
      tertanggung7 = edTertanggung7.getText().toString(); 
      tertanggung8 = edTertanggung8.getText().toString(); 
      tertanggung9 = edTertanggung9.getText().toString(); 
      tertanggung10 = edTertanggung10.getText().toString(); 

     } 

     switch(v.getId()) 
     { 
      case R.id.buttom_submit: 
       // insert data barang baru 
       barang = dataSource.createBarang(agen, no_kk, no_blanko, nama, alamat, jml_tertanggung, pilihan, telepon, tertanggung1, status1, tertanggung2, status2, tertanggung3, status3, tertanggung4, status4, tertanggung5, status5, tertanggung6, status6, tertanggung7, status7, tertanggung8, status8, tertanggung9, status9, tertanggung10, status10); 

       Intent z = new Intent(this, Otorisasi.class); 
       startActivity(z); 
       CreateData.this.finish(); 
       //konfirmasi kesuksesan 
       Toast.makeText(this, "masuk Entri\n" + 
         "id_agen:" + barang.getAgen() + 
         "no_kk:" + barang.getNo_kk() + 
         "no_blanko:" + barang.getNo_blanko() + 
         "nama:" + barang.getNama() + 
         "alamat:" + barang.getAlamat() + 
         "jml_tertanggung:" + barang.getJml_tertanggung() + 
         "pilihan:" + barang.getPilihan_paket() + 
         "telepon:" + barang.getTelepon() + 
         "tertanggung1:" + barang.getTertanggung1() + 
         "status1:" + barang.getStatus1() + 
         "tertanggung2:" + barang.getTertanggung2() + 
         "status2:" + barang.getStatus2() + 
         "tertanggung3:" + barang.getTertanggung3() + 
         "status3:" + barang.getStatus3() + 
         "tertanggung4:" + barang.getTertanggung4() + 
         "status4:" + barang.getStatus4() + 
         "tertanggung5:" + barang.getTertanggung5() + 
         "status5:" + barang.getStatus5() + 
         "tertanggung6:" + barang.getTertanggung6() + 
         "status6:" + barang.getStatus6(), Toast.LENGTH_LONG).show(); 
       break; 

     } 
    } 

請看變量阿根和edAgen。我嘗試在.txt文件設置字段從價值

回答

1
<EditText 
    android:id="@+id/edtText" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

您的代碼看起來像這樣:

//Find the directory for the SD Card using the API 
//*Don't* hardcode "/sdcard" 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"file.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
    br.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 

//Find the view by its id 
EditText edtText = (EditText)findViewById(R.id.edtText); 

//Set the text 
edtText.setText(text); 
+0

thx man ,, i apreciate。但是當我在我的編輯文本xml中使用提示時,出現的文本是我的提示不是文件中的值.txt –

+0

試試這個.... edtText.setText(text.toString()); – thet

+0

thx夥計它的作品! –