0

I am building an android application that stores data in a json file locally and reads/displays the data when the user opens the app. I have successfully done the code for writing json objects into json arrays which are stored in the internal storage of my android device. My problem is to get the app to read those json objects back into the device, specifically into a textview(s).

Here is how the json array looks like:

[
    {
        "Record Group Code":"RepGrpID",
        "Province":"Central",
        "District":"Kairuku",
        "Facility":"Upulima SC",
        "Date":"09\/03\/2023"
    }
]

Here is a piece of code from my MainActivity.java where I created the readfile().

package com.example.covid_19vaccination;

import ...


public class MainActivity extends AppCompatActivity {

    private static final String FILE_NAME = "covid_data";

    TextView TextViewRGCode, TextViewProvince, TextViewDistrict, TexviewFacility, TextViewDate;

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

        readFile();


        TextViewRGCode = findViewById(R.id.trRGC);
        TextViewProvince = findViewById(R.id.trProv);
        TextViewDistrict = findViewById(R.id.trDist);
        TextViewFacility = findViewById(R.id.trFac);
        TextViewDate = findViewById(R.id.trDate);

        ...

    }
}
       

and for the readFile() I did this:

private void readFile() {

        File file = new File(this.getFilesDir(),FILE_NAME);
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuilder stringBuilder = new StringBuilder();
            String line = bufferedReader.readLine();

            while (line != null) {

                stringBuilder.append(line).append("\n");
                line = bufferedReader.readLine();

            }
            bufferedReader.close();


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

this is where I am stuck. I want to get/read the values of each json object from the file from the device internal storage and display in their respective Textview widgets but I don't know what to do from here and can't find a clear and direct approach for this on the internet. I am very new to android and Java and if anyone would help me out with an example or clear code for achieving that that I would appreciate it.

  • [Parse the data from the json file](https://devqa.io/how-to-parse-json-in-java/) and then [set the text in the respective textviews](https://stackoverflow.com/questions/19452269/android-set-text-to-textview). – Alias Cartellano Mar 10 '23 at 18:50

0 Answers0