How To Use Google Sheet As Database for Android App -5 Displaying Item Details

This is continuation to first part How To Use Google Sheet As Database for Android App – 3 Read Operation .

 

Here I am going to demonstrate on displaying Google sheet content in Android list view.

 

Please watch below video along with tutorial to avoid errors, don’t skip any part.

 

 

[maxbutton id=”6″ ]

or

Open this link in your phone browser – http://bit.ly/2wyBjS7

 

Demo Sheet Url – http://bit.ly/2ZbVly5

 

 

 

Step 1 : Add Item Id to the list which was missed in previous tutorial. Watch it in video carefully. You can refer updated ItemList.java file in step

 

Step 2: Add item_details.xml file which display the individual item details which is clicked from list

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="112dp"
        android:text=" Item Name :"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="60dp"
        android:text=" Item Id :"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="92dp"
        android:layout_marginTop="112dp"
        android:text="name"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="124dp"
        android:layout_marginTop="60dp"
        android:text="id"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:text="Brand :"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView
        android:id="@+id/tv_brand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:text="brand"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="@+id/tv_item_name"
        app:layout_constraintTop_toBottomOf="@+id/tv_item_name" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Price :"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/textView6" />

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="price"
        android:textColor="#000"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="@+id/tv_brand"
        app:layout_constraintTop_toBottomOf="@+id/tv_brand" />

</android.support.constraint.ConstraintLayout>

 

Step 3 : Add ItemDetails.java file which binds xml objects and display the details

 

package com.ccc.gsheetitemdemo2;



import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ItemDetails extends AppCompatActivity  {


    TextView textViewitemName, textViewbrand, textViewprice,textViewId;
    Button buttonUpdateItem, buttonDeleteItem;
    String itemId, itemName, brand, price;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.item_details);

        Intent intent = getIntent();
        itemId = intent.getStringExtra("itemId");
        itemName = intent.getStringExtra("itemName");
        brand = intent.getStringExtra("brand");
        price = intent.getStringExtra("price");

        textViewId = (TextView)findViewById(R.id.tv_id);
        textViewitemName = (TextView) findViewById(R.id.tv_item_name);
        textViewbrand = (TextView) findViewById(R.id.tv_brand);
        textViewprice = (TextView) findViewById(R.id.tv_price);

        textViewId.setText(itemId);
        textViewitemName.setText(itemName);
        textViewbrand.setText(brand);
        textViewprice.setText(price);


    }
}<br><br><br>

Step 4: Add onItemClick method which is method implemented for

AdapterView OnItemClickListener

 

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ItemDetails.class);
        HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
        String itemId = map.get("itemId").toString();
        String itemName = map.get("itemName").toString();
        String brand = map.get("brand").toString();
        String price = map.get("price").toString();


        // String sno = map.get("sno").toString();

        // Log.e("SNO test",sno);
        intent.putExtra("itemId",itemId);
        intent.putExtra("itemName",itemName);
        intent.putExtra("brand",brand);
        intent.putExtra("price",price);


        startActivity(intent);
    }

 

Add listview item click listner

 

<span>listView</span>.setOnItemClickListener(<span>this</span>);

 

 

Refer Complete ItemList.java

 

package com.ccc.gsheetitemdemo2;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ListItem extends AppCompatActivity implements AdapterView.OnItemClickListener {


    ListView listView;
    SimpleAdapter adapter;
    ProgressDialog loading;
    EditText editTextSearchItem;

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

        listView = (ListView) findViewById(R.id.lv_items);

        listView.setOnItemClickListener(this);

        editTextSearchItem = (EditText)findViewById(R.id.et_search);

        getItems();

    }


    private void getItems() {

        loading =  ProgressDialog.show(this,"Loading","please wait",false,true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "Your Web App URL/exec?action=getItems",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseItems(response);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        int socketTimeOut = 50000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

        stringRequest.setRetryPolicy(policy);

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);

    }


    private void parseItems(String jsonResposnce) {

        ArrayList<HashMap<String, String>> list = new ArrayList<>();

        try {
            JSONObject jobj = new JSONObject(jsonResposnce);
            JSONArray jarray = jobj.getJSONArray("items");


            for (int i = 0; i < jarray.length(); i++) {

                JSONObject jo = jarray.getJSONObject(i);

                String itemId = jo.getString("itemId");
                String itemName = jo.getString("itemName");
                String brand = jo.getString("brand");
                String price = jo.getString("price");


                HashMap<String, String> item = new HashMap<>();
                item.put("itemId",itemId);
                item.put("itemName", itemName);
                item.put("brand", brand);
                item.put("price",price);


                list.add(item);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
                new String[]{"itemName","brand","price","itemId"},new int[]{R.id.tv_item_name,R.id.tv_brand,R.id.tv_price});


        listView.setAdapter(adapter);
        loading.dismiss();

        editTextSearchItem.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                ListItem.this.adapter.getFilter().filter(charSequence);

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }


    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ItemDetails.class);
        HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
        String itemId = map.get("itemId").toString();
        String itemName = map.get("itemName").toString();
        String brand = map.get("brand").toString();
        String price = map.get("price").toString();


        // String sno = map.get("sno").toString();

        // Log.e("SNO test",sno);
        intent.putExtra("itemId",itemId);
        intent.putExtra("itemName",itemName);
        intent.putExtra("brand",brand);
        intent.putExtra("price",price);


        startActivity(intent);
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}

 

Step 5: update in manifest file

 

Step 6: Run and check for output