Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186189
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T18:41:20+00:00 2022-06-10T18:41:20+00:00

java – Offline Function Android Studio

  • 0

[ad_1]

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>

[ad_2]

  • 0 0 Answers
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 396 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 370 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 361 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.