0

我有這個項目,它使用兩個控制TextViewsEditTextsFloatingActionButtons顯示人員保險信息。我正在嘗試將此信息保存到移動設備的內部/外部存儲器中。我的代碼只是當信息顯示時,所有的Strings都被附加爲一個大的String。這裏是我的意思的打印屏幕。從內部存儲器顯示多個字符串的問題

As you can see all the strings that should be in their correspondent places are all in one big string that gets displayed in all the <code>TextViews</code>.

這裏是我用於在java文件中FloatingActionButtons稱爲InsuranceActivity.java代碼:

//Saves information on the editText fields 
    FloatingActionButton saveInfo = (FloatingActionButton) findViewById(R.id.saveInfo); 
    saveInfo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String InsurerNameStr = InsurerNameEdit.getText().toString(); 
      String MembershipStr = MembershipNoEdit.getText().toString(); 
      String NameStr = NameEdit.getText().toString(); 
      String DOBStr = DOBEdit.getText().toString(); 
      String PostcodeStr = PostcodeEdit.getText().toString(); 
      String file_name = "InsuranceInfo"; 
      try { 
       FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE); 
       fileOutputStream.write(InsurerNameStr.getBytes()); 
       fileOutputStream.write(MembershipStr.getBytes()); 
       fileOutputStream.write(NameStr.getBytes()); 
       fileOutputStream.write(DOBStr.getBytes()); 
       fileOutputStream.write(PostcodeStr.getBytes()); 
       fileOutputStream.close(); 
       Toast.makeText(getApplicationContext(), "Information Saved.", Toast.LENGTH_LONG).show(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    //Hides EditText and displays TextViews with the information that was saved 
    FloatingActionButton showInfo = (FloatingActionButton) findViewById(R.id.showInfo); 
    showInfo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      InsurerNameEdit.setVisibility(View.GONE); 
      MembershipNoEdit.setVisibility(View.GONE); 
      NameEdit.setVisibility(View.GONE); 
      DOBEdit.setVisibility(View.GONE); 
      PostcodeEdit.setVisibility(View.GONE); 
      InsurerName.setVisibility(View.VISIBLE); 
      MembershipNo.setVisibility(View.VISIBLE); 
      Name.setVisibility(View.VISIBLE); 
      DOB.setVisibility(View.VISIBLE); 
      Postcode.setVisibility(View.VISIBLE); 

      try { 
       String InsurerNameStr; 
       String MembershipStr; 
       String NameStr; 
       String DOBStr; 
       String PostcodeStr; 

       FileInputStream fileInputStream = openFileInput("InsuranceInfo"); 
       InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); 
       BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
       StringBuffer stringBuffer = new StringBuffer(); 

       while ((InsurerNameStr = bufferedReader.readLine()) != null) { 
        stringBuffer.append(InsurerNameStr); 
       } 
       while ((MembershipStr = bufferedReader.readLine()) != null) { 
        stringBuffer.append(MembershipStr); 
       } 
       while ((NameStr = bufferedReader.readLine()) != null) { 
        stringBuffer.append(NameStr); 
       } 
       while ((DOBStr = bufferedReader.readLine()) != null) { 
        stringBuffer.append(DOBStr); 
       } 
       while ((PostcodeStr = bufferedReader.readLine()) != null) { 
        stringBuffer.append(PostcodeStr); 
       } 

       InsurerName.setText(stringBuffer.toString()); 
       MembershipNo.setText(stringBuffer.toString()); 
       Name.setText(stringBuffer.toString()); 
       DOB.setText(stringBuffer.toString()); 
       Postcode.setText(stringBuffer.toString()); 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

感謝您對您的幫助提前。

回答

0

您的代碼完全按照您的要求進行操作。先閱讀文件的內容爲StringBuffer,然後在下面的行,你設置每個TextView以顯示StringBuffer的內容:如果你希望每個TextView顯示不同的東西

InsurerName.setText(stringBuffer.toString()); 
MembershipNo.setText(stringBuffer.toString()); 
Name.setText(stringBuffer.toString()); 
DOB.setText(stringBuffer.toString()); 
Postcode.setText(stringBuffer.toString()); 

,你應該分別讀取文件的每一行,並在適當的TextView中顯示每行的內容。更好的是,以不依賴寫入順序的JSON格式序列化數據,並且可以輕鬆查詢特定值。

編輯

在你的情況,你可以嘗試這樣的事:

. 
. 
. 
FileInputStream fileInputStream = openFileInput("InsuranceInfo"); 
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); 
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 

InsurerName.setText(bufferedReader.readLine()); 
MembershipNo.setText(bufferedReader.readLine()); 
. 
. 
. 

當然,這種方法假定您的文件將永遠遵循正確的格式,總是有行權數量等等。如果用戶恰好在EditText中創建了一個新行並隨後保存,則整個方法將會中斷。我可能不會在製作應用程序中使用它,但它應該足以讓您指向正確的方向。

(小型的Java尖,變量名不應該以大寫字母開頭,按照慣例,只有類/接口名稱應該以大寫字母開頭

+0

謝謝您的幫助,我不知道是不是我!在這裏要求太多,但有什麼樣的例子,我可以在每個'TextView'上顯示不同的東西,或者你可以指向正確的方向嗎?我對此很陌生。 –

+0

我真的不知道如果你想在每個「TextView」中顯示不同的字符串,你必須通過'setText()'傳遞一個不同的字符串給你的每一個字符串。在你的情況下,你需要讀一個行,將該行設置爲相應的「TextView」的文本,然後重複。我可以嘗試在示例中進行編輯。 –

相關問題