Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to filter the specific json data in android studio,instead of getting all the data.For example filter the data based on 16-3-2019 date.Below is the json data

JSON
matches
0
unique_id : 1168120
date : "2019-03-15T00:00:00.000Z"
dateTimeGMT : "2019-03-15T04:30:00.000Z"
team-1 : "Afghanistan"
team-2 : "Ireland"
type : "Test"
squad : true
toss_winner_team : "Ireland"
winner_team : "Afghanistan"
matchStarted : true

1
unique_id : 1160301
date : "2019-03-16T00:00:00.000Z"
dateTimeGMT : "2019-03-16T21:30:00.000Z"
team-1 : "Northern Districts"
team-2 : "Central Districts"
toss_winner_team : "Northern Districts"
type : "First-class"
squad : false
matchStarted : true


What I have tried:

Below is the java code

Java
<pre>public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<Model> modelList;

    private String url = "http://cricapi.com/api/matches?apikey=wnUZ9BJ9keX6It8FQQryK6mbCZo1";



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

        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        modelList = new ArrayList<>();

        loadUrlData();


    }

    private void loadUrlData() {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage("Loading ...");
        pd.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pd.dismiss();
                try {
                    JSONArray jsonArray=new JSONObject(response).getJSONArray("matches");
                    for (int i=0; i<jsonArray.length(); i++){
                        try {
                            String uniqueid=jsonArray.getJSONObject(i).getString("unique_id");
                            String team1=jsonArray.getJSONObject(i).getString("team-1");
                            String team2=jsonArray.getJSONObject(i).getString("team-2");
                            String matchType=jsonArray.getJSONObject(i).getString("type");
                            String matchStatus=jsonArray.getJSONObject(i).getString("matchStarted");
                            if (matchStatus.equals("true")){
                                matchStatus="Match Started";
                            }else {
                                matchStatus="Match not started";
                            }
                            String dateTimeGMT=jsonArray.getJSONObject(i).getString("dateTimeGMT");
                            SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                            format1.setTimeZone(TimeZone.getTimeZone(dateTimeGMT));
                            Date date=format1.parse(dateTimeGMT);

                            SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            format2.setTimeZone(TimeZone.getTimeZone("GMT"));

                            String dateTime=format2.format(date);


                            Model model=new Model(uniqueid,team1,team2,matchType,matchStatus,dateTime);
                            modelList.add(model);


                        }catch (Exception e){
                            Toast.makeText(MainActivity.this,"" +e.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    adapter=new MyAdapter(modelList,getApplicationContext());
                    recyclerView.setAdapter(adapter);

                }catch (Exception e){
                    Toast.makeText(MainActivity.this,"" +e.getMessage(),Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,"Error:" +error,Toast.LENGTH_LONG).show();
            }
        });
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }


}
Posted
Comments
David Crow 18-Mar-19 17:03pm    
Have you tried not creating a new Model object and adding it to modelList if dateTime is not what you are looking for?

In other words, there is no if() condition around the call to add(). The matches are being added regardless of date.
Member 14186786 19-Mar-19 11:42am    
Yes i have created the model named as Model.Below is the java code of the Model class

public class Model {

String id,team1,team2,matchType,matchStatus,date;

public Model(String id, String team1, String team2, String matchType, String matchStatus, String date) {
this.id = id;
this.team1 = team1;
this.team2 = team2;
this.matchType = matchType;
this.matchStatus = matchStatus;
this.date = date;
}

public String getId() {
return id;
}

public String getTeam1() {
return team1;
}

public String getTeam2() {
return team2;
}

public String getMatchType() {
return matchType;
}

public String getMatchStatus() {
return matchStatus;
}

public String getDate() {
return date;
}
}
David Crow 19-Mar-19 12:07pm    
You might want to re-read my response to you. I did not ask what Model looked like.
Member 14186786 19-Mar-19 12:32pm    
Sorry for this response, no i did not create without new model

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900