从内部存储显示多个字符串的问题

时间:2016-03-27 00:10:51

标签: android android-studio bufferedreader fileinputstream stringbuffer

我有这个项目,它使用控制FloatingActionButtonsTextViews的两个EditTexts显示人员保险信息。我试图将此信息保存到移动设备的内部/外部存储。我的代码只是当信息显示时,所有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>.

以下是我在名为FloatingActionButtons的java文件中用于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();
            }
        }
    });

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码完全按照您的要求进行操作。您首先将文件内容读入StringBuffer,然后在下面的行中设置每个TextView以显示StringBuffer的内容:

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

如果您希望每个TextView显示不同的内容,则应分别阅读文件的每一行,并在相应的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提示,变量名永远不应以大写字母开头;按照惯例,只有类/接口名称应以大写字母开头。