尝试保存多个EditText值,然后使用Try and Catch显示它们

时间:2016-03-15 04:58:02

标签: android

我需要能够显示保存到设备内部存储的EditText字段中的多个已保存条目。输入数据时,我已在EditText字段中显示3个不同的TextView字段。现在我需要显示新条目而不替换显示的旧条目。我应该用其他东西替换 txType.setText(temp)中的 temp 吗?以下是我的代码.....

public class MainActivity extends AppCompatActivity {

EditText etType, etMiles, etDate;
Button btAdd;
TextView txType, txMiles, txDate;

String dataType, dataMiles, dataDate;
private String fileType = "myTypeData";
private String fileMiles = "myMilesData";
private String fileDate = "myDateData";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etType=(EditText)findViewById(R.id.etType);
    etMiles=(EditText)findViewById(R.id.etMiles);
    etDate=(EditText)findViewById(R.id.etDate);

    txType=(TextView)findViewById(R.id.txType);
    txMiles=(TextView)findViewById(R.id.txMiles);
    txDate=(TextView)findViewById(R.id.txDate);

    btAdd=(Button)findViewById(R.id.btAdd);
    btAdd.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            dataType=etType.getText().toString();
            try {
                FileOutputStream fOut = openFileOutput(fileType,MODE_WORLD_READABLE);
                fOut.write(dataType.getBytes());
                fOut.close();

            }
            catch (Exception e){
                e.printStackTrace();
            }
            try{
                FileInputStream fin = openFileInput(fileType);
                int c;
                String temp="";

                while( (c = fin.read()) != -1){
                    temp = temp + Character.toString((char)c);
                }
                txType.setText(temp);
            }
            catch(Exception e){
            }

            dataMiles=etMiles.getText().toString();
            try {
                FileOutputStream fOut = openFileOutput(fileMiles,MODE_WORLD_READABLE);
                fOut.write(dataMiles.getBytes());
                fOut.close();

            }
            catch (Exception e){
                e.printStackTrace();
            }
            try{
                FileInputStream fin = openFileInput(fileMiles);
                int c;
                String temp="";

                while( (c = fin.read()) != -1){
                    temp = temp + Character.toString((char)c);
                }
                txMiles.setText(temp);
            }
            catch(Exception e){
            }

            dataDate=etDate.getText().toString();
            try {
                FileOutputStream fOut = openFileOutput(fileDate,MODE_WORLD_READABLE);
                fOut.write(dataDate.getBytes());
                fOut.close();

            }
            catch (Exception e){
                e.printStackTrace();
            }
            try{
                FileInputStream fin = openFileInput(fileDate);
                int c;
                String temp="";

                while( (c = fin.read()) != -1){
                    temp = temp + Character.toString((char)c);
                }
                txDate.setText(temp);
                Toast.makeText(getBaseContext(),"Workout Saved",Toast.LENGTH_SHORT).show();
              }
            catch(Exception e){
              }

          }
      });
  }
}

0 个答案:

没有答案