`
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
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;
public class ListofCars extends AppCompatActivity implements
AdapterView.OnItemClickListener {
ListView listView;
SimpleAdapter adapter;
ProgressDialog loading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carslist);
listView = findViewById(R.id.listof_cars);
listView.setOnItemClickListener(this);
getItems();
}
private void getItems() {
loading = ProgressDialog.show(this, "Loading", "please wait", false, true);
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://script.google.com/macros/s/AKfycbxOLElujQcy1-
ZUer1KgEvK16gkTLUqYftApjNCM_IRTL3HSuDk/exec?
id=YOUR_SPREAD_SHEET_IDHERE&sheet=YOURSHEETNAMEHERE",
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);
}
void parseItems(String jsonResponse) {
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonResponse);
JSONArray jarray = jobj.getJSONArray("listofcars");
for (int i = 0; i < jarray.length(); i++) {
JSONObject jo = jarray.getJSONObject(i);
String car = jo.getString("car");
String type = jo.getString("type");
HashMap<String, String> item = new HashMap<>();
item.put("car", car);
item.put("type", type);
list.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(this,list, R.layout.list_item_row,
new String[]{"car","type"},new int[]{R.id.car_name,R.id.car_type});
listView.setAdapter(adapter);
loading.dismiss();
}
public void onItemClick(@Nullable AdapterView<?> parent, @Nullable
View view, int position, long id) {
Intent intent = new Intent(this, Cardetails.class);
HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
String car = map.get("car");
String type = map.get("type");
intent.putExtra("car",car);
intent.putExtra("type",type);
startActivity(intent);
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {}}
`