enter image description here This is what I am getting with the help Sultan Mahmud help(he has posted below in the comment) . But from the code posted here I just get blank layout This is what I want enter image description here I am trying to read the data from the JSON and parse them in android layout. The basic idea of this overall is I am trying to group the items by date. This means all the events occurred during that particular date is listed below it and that date is listed as a header.
I also added the print statement inside JSON method to see whether the objects are added to it or not. And it turns out the object is added to the list. There might be some issue of setting the adapter or the way to call the adapter in JSON. Not sure though
For this, I got the help from this post Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date
I tried it with dummy data and it works fine. But when I read it from JSON file and trying to adopt in android I might have done some mistake.
What I am doing is parseJSOn method is creating the object and adding into the ArrayList. And that ArrayList is passed into the hashmap? Is this the way to do? I have seen lots of tutorials posted about JSON parsing, but haven't really got the idea how to do when there are multiple view types in my case header and events
Also, Can anyone help me how to implement onclick listener recycler view in my case? (In most of the case model is passed as reference for getting the position. so what would be in mine case?
I have posted the code with an explanation. Any help is appreciated
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Adapter;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import static java.util.stream.Collectors.toMap;
public class MainActivity extends AppCompatActivity {
@NonNull
private List<StreamItem> myOptions = new ArrayList<>();
/*
In this we have collection of list of header and Event it means for each date
list of events are added. I have two layout of them one of header and one for event.
*/
List<ListItem> consolidatedList = new ArrayList<>();
private RecyclerView mRecyclerView;
private StreamArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
/*
what I am doing is I am calling the parsJSON method what it does is it create the object for my model and add them in list
*/
parseJSON();
/*
This the dummy data I tried it word as fine and group the datas by date
*/
/* myOptions.add(new StreamItem("name 1", "2016-06-21"));
myOptions.add(new StreamItem("name 2", "2016-06-05"));
myOptions.add(new StreamItem("name 2", "2016-06-05"));
myOptions.add(new StreamItem("name 3", "2016-05-17"));
myOptions.add(new StreamItem("name 3", "2016-05-17"));
myOptions.add(new StreamItem("name 3", "2016-05-17"));
myOptions.add(new StreamItem("name 3", "2016-05-17"));
myOptions.add(new StreamItem("name 2", "2016-06-05"));
myOptions.add(new StreamItem("name 3", "2016-05-17"));*/
HashMap<String, List<StreamItem>> groupedHashMap = groupDataIntoHashMap(myOptions);
for (String date : groupedHashMap.keySet()) {
HeaderItem dateItem = new HeaderItem();
dateItem.setDate(date);
consolidatedList.add(dateItem);
for (StreamItem pojoOfJsonArray : groupedHashMap.get(date)) {
EventItem generalItem = new EventItem();
generalItem.setStreamItem(pojoOfJsonArray);//setBookingDataTabs(bookingDataTabs);
consolidatedList.add(generalItem);
}
}
adapter = new StreamArrayAdapter(this, consolidatedList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
}
private HashMap<String, List<StreamItem>> groupDataIntoHashMap(List<StreamItem> listOfPojosOfJsonArray) {
HashMap<String, List<StreamItem>> groupedHashMap = new HashMap<>();
for (StreamItem pojoOfJsonArray : listOfPojosOfJsonArray) {
String hashMapKey = pojoOfJsonArray.getDate();
if (groupedHashMap.containsKey(hashMapKey)) {
// The key is already in the HashMap; add the pojo object
// against the existing key.
groupedHashMap.get(hashMapKey).add(pojoOfJsonArray);
} else {
// The key is not there in the HashMap; create a new key-value pair
List<StreamItem> list = new ArrayList<>();
list.add(pojoOfJsonArray);
groupedHashMap.put(hashMapKey, list);
}
}
return groupedHashMap;
}
private void parseJSON() {
final List<StreamItem> events = new ArrayList<>();
String url = "https://netsync.unl.edu/feeds/apps/capitol_live/";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject e = response.getJSONObject(i);
String date = e.getString("streamStart");
String des = e.getString("desc");
String image = e.getString("imgName");
String title = e.getString("category");
StreamItem str = new StreamItem(date, title, des, image);
myOptions.add(str);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
}
enter code here
This is my data model POJO
public class StreamItem {
public String date;
public String title;
public String desc;
public String image;
public String url;
public StreamItem(String date , String title , String desc , String image , String url) {
this.date = date;
this.title = title;
this.desc = desc;
this.image = image;
this.url = url;
}
public String getUrl(){
return url;
}
public void setUrl(String url){
this.url = url;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}