복구코드

장원녕·2023년 5월 30일

package com.example.ms15;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class DetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);

    // Retrieve the intent data
    Intent intent = getIntent();
    int rawFileResourceId = intent.getIntExtra("rawFileResourceId", 0);

    try {
        InputStream inputStream = getResources().openRawResource(rawFileResourceId);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder jsonData = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonData.append(line);
        }
        reader.close();

        JSONArray jsonArray = new JSONArray(jsonData.toString());
        JSONObject spotObject = jsonArray.getJSONObject(0); // Assuming you want to retrieve details for the first spot
        String spotKey = spotObject.keys().next();
        JSONObject spotDetails = spotObject.getJSONObject(spotKey);

        JSONArray admissionFeeArray = spotDetails.getJSONArray("Admission Fee");
        JSONArray timeArray = spotDetails.getJSONArray("Time");
        JSONArray parkPriceArray = spotDetails.getJSONArray("ParkPrice");

        List<String> admissionFeeList = jsonArrayToList(admissionFeeArray);
        List<String> timeList = jsonArrayToList(timeArray);
        List<String> parkPriceList = jsonArrayToList(parkPriceArray);

        String admissionFees = TextUtils.join(", ", admissionFeeList);
        String timeValues = TextUtils.join(", ", timeList);
        String parkPrices = TextUtils.join(", ", parkPriceList);

        TextView admissionFeesTextView = findViewById(R.id.admissionFeesTextView);
        TextView timeTextView = findViewById(R.id.timeTextView);
        TextView parkPriceTextView = findViewById(R.id.parkPriceTextView);

        admissionFeesTextView.setText(admissionFees);
        timeTextView.setText(timeValues);
        parkPriceTextView.setText(parkPrices);




        // Do whatever you want with the retrieved data (e.g., display in views, store in variables)


    } catch (JSONException | IOException e) {
        throw new RuntimeException(e);
    }
}

private List<String> jsonArrayToList(JSONArray jsonArray) throws JSONException {
    List<String> list = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
        list.add(jsonArray.getString(i));
    }
    return list;
}

}

profile
희망찬 개발자

0개의 댓글