简体   繁体   中英

button not working in android basic apo I am trying to create

I am having an issue with my first app. When I click on "Submit" it is meant to create a textfile with the values that the user has entered however it is not creating it. I am not sure what the issue is.

I would also like to know if there is a better way of storing the data entered by the user to make is useable eventually in a excel spread sheet. Or just use plain text file to make it easy?

package com.example.mybodyrecord;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    String myBodyRecord = "My_Body_Record.txt";
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    File file; // declare here and initialize inside onCreate.

    Button button;

    EditText weightInput;
    EditText ketoneBloodInput;
    EditText ketoneUrineInput;
    EditText bloodPressureInput;
    EditText bloodSugarInput;

    String weight;
    String ketoneBlood;
    String ketoneUrine;
    String bloodPressure;
    String bloodSugar;


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

        //Setting where to get the data from, ie what fields
        weightInput = findViewById(R.id.weight);
        ketoneBloodInput = findViewById(R.id.ketoneblood);
        ketoneUrineInput = findViewById(R.id.ketoneurine);
        bloodPressureInput = findViewById(R.id.bp);
        bloodSugarInput = findViewById(R.id.bslvl);

        button = findViewById(R.id.submit);

        //activity has a context now you can use getApplicationContext or this
        file = new File(this.getFilesDir(),myBodyRecord);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FileOutputStream fos = null;

                //Taking input from the fields
                weight = weightInput.getText().toString() +"kg";
                ketoneBlood = ketoneBloodInput.getText().toString() +"mmol/L";
                ketoneUrine = ketoneUrineInput.getText().toString();
                bloodPressure = bloodPressureInput.getText().toString() +"mmHg";
                bloodSugar = bloodSugarInput.getText().toString() +"mmol/L";

                //Save to the file, with the date and time
                if (file.exists()) {
                    try {
                        fos = openFileOutput(myBodyRecord, getApplicationContext().MODE_APPEND);
                        fos.write("\n\r".getBytes());
                        fos.write(formatter.format(date).getBytes());
                        fos.write(weight.getBytes());
                        fos.write(ketoneBlood.getBytes());
                        fos.write(ketoneUrine.getBytes());
                        fos.write(bloodPressure.getBytes());
                        fos.write(bloodSugar.getBytes());

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                else {
                    try {
                        fos = openFileOutput(myBodyRecord, getApplicationContext().MODE_PRIVATE);
                        fos.write("\n\r".getBytes());
                        fos.write(formatter.format(date).getBytes());
                        fos.write(weight.getBytes());
                        fos.write(ketoneBlood.getBytes());
                        fos.write(ketoneUrine.getBytes());
                        fos.write(bloodPressure.getBytes());
                        fos.write(bloodSugar.getBytes());

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
}

Collins

The question how to read/write to text files in android is already answered in this thread:

How can I read a text file in Android?

However, tis is not what you want in android. First, you should be familiar with the concept of application and service. Then, to solve your problem:
- For a smaller amount of data (eg. settings) you might use SharedPreferences.
- For more data (eg. records) the recommended approach is a ContentProvider with SQLite database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM