Search in sources :

Example 1 with SponserAdapter

use of com.manan.dev.ec2018app.Adapters.SponserAdapter in project EC2018App by Manan-YMCA.

the class SponsorsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sponsors);
    sponserList = new ArrayList<>();
    progressBar = new ProgressDialog(this);
    progressBar.setMessage("Loading Sponsors...");
    // progressBar.setCancelable(false);
    progressBar.setCanceledOnTouchOutside(false);
    progressBar.show();
    noSponTV = findViewById(R.id.tv_no_spon);
    noSponTV.setVisibility(View.GONE);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(SponsorsActivity.this));
    mAdapter = new SponserAdapter(SponsorsActivity.this, sponserList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    findViewById(R.id.spon_back_button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    if (isNetworkAvailable()) {
        RequestQueue queue = Volley.newRequestQueue(SponsorsActivity.this);
        String url = "https://elementsculmyca2018.herokuapp.com/api/v1/sponsors/getsponsors";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    JSONArray sponsersArray = object.getJSONArray("data");
                    for (int i = 0; i < sponsersArray.length(); i++) {
                        Sponsers sponsers = new Sponsers();
                        JSONObject currEvent = sponsersArray.getJSONObject(i);
                        if (currEvent.has("name")) {
                            sponsers.setSname(currEvent.getString("name"));
                        }
                        if (currEvent.has("title"))
                            sponsers.setTitle(currEvent.getString("title"));
                        if (currEvent.has("_id"))
                            sponsers.setId(currEvent.getString("_id"));
                        if (currEvent.has("rank"))
                            sponsers.setRank(currEvent.getInt("rank"));
                        if (currEvent.has("logo"))
                            sponsers.setImageUrl(currEvent.getString("logo"));
                        if (currEvent.has("url"))
                            sponsers.setUrl(currEvent.getString("url"));
                        sponserList.add(sponsers);
                        progressBar.dismiss();
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                progressBar.dismiss();
                noSponTV.setVisibility(View.VISIBLE);
            }
        });
        queue.add(stringRequest);
    } else {
        progressBar.dismiss();
        noSponTV.setVisibility(View.VISIBLE);
        MDToast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT, MDToast.TYPE_ERROR).show();
    }
}
Also used : VolleyError(com.android.volley.VolleyError) SponserAdapter(com.manan.dev.ec2018app.Adapters.SponserAdapter) Sponsers(com.manan.dev.ec2018app.Models.Sponsers) StringRequest(com.android.volley.toolbox.StringRequest) JSONArray(org.json.JSONArray) ProgressDialog(android.app.ProgressDialog) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 StringRequest (com.android.volley.toolbox.StringRequest)1 SponserAdapter (com.manan.dev.ec2018app.Adapters.SponserAdapter)1 Sponsers (com.manan.dev.ec2018app.Models.Sponsers)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1