Offline Function Android Studio

Viewed 32

I've got some informations that I get from a nutrition database.I'm new to android studio and my question is, how can I access the data while being offline using Firebase? I don't know where to start, I want to display the list that I get while being offline.Any advices would help me. This is the list that I get:
enter image description here
Java code that I use to parse the url:

package com.example.myapplication;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MenuItem;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class NutritionPlan2 extends AppCompatActivity {
    
    
    
    
        private String TAG = MainActivity.class.getSimpleName();
        private ListView lv;
    
        ArrayList<HashMap<String, String>> resultList;
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
                case android.R.id.home:
                    finish();
                    break;
            }
            return true;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_nutrition_plan2);
    
    
    
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
    
            if (getSupportActionBar() != null){
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
            }
    
    
    
    
            resultList = new ArrayList<>();
            lv = (ListView) findViewById(R.id.list);
            new GetContacts().execute();
    
    
    
    
    
    
    
    
    
    
        }
    
    
    
        private class GetContacts extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(NutritionPlan2.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
    
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                HttpHandler sh = new HttpHandler();
                // Making a request to url and getting response
                String url = "https://api.spoonacular.com/recipes/complexSearch?diet=vegan&addRecipeNutrition=true&apiKey=d22842b0ca9148839497a0022902ae97";
    
                String jsonStr = sh.makeServiceCall(url);
    
    
                Log.e(TAG, "Response from url: " + jsonStr);
                if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);
    
                        // Getting JSON Array node
                        JSONArray results = jsonObj.getJSONArray("results");
    
                        // looping through All items
                        for (int i = 0; i < results.length(); i++) {
                            JSONObject c = results.getJSONObject(i);
                            String title = c.getString("title");
                            String healthScore = c.getString("healthScore");
                            String servings = c.getString("servings");
                            String pricePerServing = c.getString("pricePerServing");
                            String readyInMinutes = c.getString("readyInMinutes");
    
    
    
    
    
                            // tmp hash map for single contact
                            HashMap<String, String> result = new HashMap<>();
    
                            // adding each child node to HashMap key => value
                            result.put("title",title);
                            result.put("pricePerServing","Cost: " + pricePerServing);
                            result.put("servings","Servings: " + servings);
                            result.put("healthScore","Health Score: " +healthScore);
                            result.put("readyInMinutes","Time to cook: " +readyInMinutes +"minutes");
    
    
    
    
    
                            // adding contact to contact list
                            resultList.add(result);
    
    //
    
    
                        }
                    } catch (final JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                            }
                        });
    
                    }
    
                } else {
                    Log.e(TAG, "Couldn't get json from server.");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Couldn't get json from server. Check LogCat for possible errors!",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                ListAdapter adapter = new SimpleAdapter(NutritionPlan2.this, resultList,
                        R.layout.list_nutrition_plans, new String[]{ "title","pricePerServing","servings","healthScore","readyInMinutes"},
                        new int[]{R.id.title, R.id.pricePerServing,R.id.servings,R.id.healthscore,R.id.readyInMinutes});
                lv.setAdapter(adapter);
    
            }
        }
    
    }
                                                                                           
                                                                                                                       
                                                                                                   
                                                                                           

This is the list adapter:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">
    
    
        <TextView
    
            android:id="@+id/title"
    
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:text="Title:"/>
    
    
        <TextView
    
            android:id="@+id/healthscore"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:textColor="@color/black"
            android:text="Health Score:"/>
    
        <TextView
    
            android:id="@+id/pricePerServing"
            android:layout_width="fill_parent"
            android:paddingTop="5dp"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="pricePerServing:"/>
    
        <TextView
    
            android:id="@+id/servings"
            android:layout_width="fill_parent"
            android:paddingTop="5dp"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="Servings:"/>
    
    
        <TextView
    
            android:id="@+id/readyInMinutes"
            android:layout_width="fill_parent"
            android:paddingTop="5dp"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="readyInMinutes:"/>
    
    
    </LinearLayout>
0 Answers
Related