How to Create Dynamic Recycler View with MYSQL Database || The Developer 2.0

Hello guys in this post , You are going to learn about the way to create a dynamic recycler view. Here we use a php Script name “matches.php” to get our data in json format. And we use Volly library to fetch that json data in to the app.  We also create a separate class name Listitem to get all values.

Watch Tutorial Video:

Home.java File

package com.sanjay.pubgmaster;

import android.app.DownloadManager;
import android.app.LauncherActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
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.List;

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
     private RecyclerView recyclerView;
     private RecyclerView.LayoutManager layoutManager;
     private RecyclerView.Adapter adapter;
     private List<ListItems> listItemsList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
        recyclerView =findViewById(R.id.match_list);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        listItemsList = new ArrayList<>();
        loadRecyclerViewdata();


    }

    private void loadRecyclerViewdata() {
        final ProgressDialog progressDialog =new ProgressDialog(this);
        progressDialog.setMessage("Loading data...");
        progressDialog.show();
        String url="https://quizeapp.000webhostapp.com/PUBG/matches.php";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i=0; i<jsonArray.length();i++)
                    {
                        JSONObject o =jsonArray.getJSONObject(i) ;
                        ListItems items = new ListItems(
                                o.getString("map"),
                                o.getString("title"),
                                o.getString("date"),
                                o.getString("match_type"),
                                o.getString("entry_fees"),
                                o.getString("kill_amount"),
                                o.getString("prize"),
                                o.getString("joining")

                        ) ;

                        listItemsList.add(items);
                        adapter = new RecycelViewAdapter(listItemsList, getApplicationContext());
                        recyclerView.setAdapter(adapter);

                    }

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


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this );
        requestQueue.add(request);


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            finish();
            moveTaskToBack(true);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
          Toast.makeText(getBaseContext(), "You Click on Home",Toast.LENGTH_LONG).show();
            // Handle the camera action
        } else if (id == R.id.nav_profile) {
            Toast.makeText(getBaseContext(), "You Click on Profile",Toast.LENGTH_LONG).show();

        } else if (id == R.id.nav_wallet) {
            Toast.makeText(getBaseContext(), "You Click on Wallet",Toast.LENGTH_LONG).show();

        } else if (id == R.id.nav_result) {
            Toast.makeText(getBaseContext(), "You Click on Result",Toast.LENGTH_LONG).show();

        } else if (id == R.id.nav_share) {
            Toast.makeText(getBaseContext(), "You Click on Share",Toast.LENGTH_LONG).show();

        } else if (id == R.id.nav_contact) {
            Toast.makeText(getBaseContext(), "You Click on Conatct",Toast.LENGTH_LONG).show();

        }
        else if (id == R.id.nav_privacy) {
            Toast.makeText(getBaseContext(), "You Click on Proivacy & Policy",Toast.LENGTH_LONG).show();

        }
        else if (id == R.id.logout) {
            SharedPreferences Prefers = getSharedPreferences("com.sanjay.pubgmaster_login_status",MODE_PRIVATE);
            SharedPreferences.Editor editor= Prefers.edit();
            editor.putString("login_status","off");
            editor.commit();
            finish();
            moveTaskToBack(true);

        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

RecycelViewAdapter.java

package com.sanjay.pubgmaster;

import android.app.LauncherActivity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

public class RecycelViewAdapter extends RecyclerView.Adapter<RecycelViewAdapter.MatchViewHolder>{
    private List<ListItems> listItemsList;
    private Context context;

    public RecycelViewAdapter( List<ListItems> listItemsList, Context context ) {
        this.listItemsList=listItemsList;
        this.context = context;

    }
    @NonNull
    @Override
    public MatchViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.matches,viewGroup,false );
        MatchViewHolder matchViewHolder = new MatchViewHolder(view);
        return matchViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MatchViewHolder viewHolder, int i) {
        ListItems listItems = listItemsList.get(i);

        String check_map=listItems.getMap();
        if(check_map.contains("Erangel") )
        {
            viewHolder.map.setImageResource(R.drawable.erangel);
        }
        else if (check_map.contains("Miramar") )
        {
            viewHolder.map.setImageResource(R.drawable.miramar);
        }
        else if (check_map.contains("Sanhok") )
        {
            viewHolder.map.setImageResource(R.drawable.sanhok);
        }
        else
        {
            viewHolder.map.setImageResource(R.drawable.vikendi);
        }

        viewHolder.match_title.setText(listItems.getTitle());
        viewHolder.Fees.setText(listItems.getEntry_fess());
        viewHolder.Kill_amount.setText(listItems.getKill_amount());
        viewHolder.prize.setText(listItems.getPrize());
        viewHolder.Date.setText(listItems.getDate());
        viewHolder.Match_type.setText(listItems.getMatch_type());
        viewHolder.joining.setText(listItems.getJoing() +"/100");
        viewHolder.join.setProgress(Integer.parseInt(listItems.getJoing()) );

    }

    @Override
    public int getItemCount() {
        return listItemsList.size();
    }

   public static class MatchViewHolder extends RecyclerView.ViewHolder {
        ImageView map;
        TextView match_title,Date,Match_type,Fees,Kill_amount,prize,joining;
        ProgressBar join;


       public MatchViewHolder(@NonNull View itemView) {
           super(itemView);
           map= itemView.findViewById(R.id.map);
           match_title=itemView.findViewById(R.id.match_title);
           Fees=itemView.findViewById(R.id.entryfee);
           Kill_amount =itemView.findViewById(R.id.killamount);
           prize=itemView.findViewById(R.id.prize);
           Date=itemView.findViewById(R.id.date);
           Match_type = itemView.findViewById(R.id.match_type);
           joining =itemView.findViewById(R.id.joining);
           join =itemView.findViewById(R.id.joining_bar);

       }
   }
}

ListItems.java

package com.sanjay.pubgmaster;

public class ListItems {
    private String map,title,date,match_type,entry_fess,kill_amount,prize,joing;

    public ListItems(String map, String title, String date, String match_type, String entry_fess, String kill_amount, String prize, String joing) {
        this.map = map;
        this.title = title;
        this.date = date;
        this.match_type = match_type;
        this.entry_fess = entry_fess;
        this.kill_amount = kill_amount;
        this.prize = prize;
        this.joing = joing;
    }

    public String getMap() {
        return map;
    }

    public String getTitle() {
        return title;
    }

    public String getDate() {
        return date;
    }

    public String getMatch_type() {
        return match_type;
    }

    public String getEntry_fess() {
        return entry_fess;
    }

    public String getKill_amount() {
        return kill_amount;
    }

    public String getPrize() {
        return prize;
    }

    public String getJoing() {
        return joing;
    }
}

Click on Below button and complete task showed in pop up to Download the matches.php file