enter image description here这是我在苏丹·马哈茂德(Sultan Mahmud)帮助下得到的(他在评论中发布了以下内容)。但是从这里发布的代码中,我只会得到空白的布局 这就是我要的 enter image description here 我正在尝试从JSON读取数据并在android布局中解析它们。总体而言,我的基本思想是按日期对项目进行分组。这意味着在该特定日期发生的所有事件都列在其下方,并且该日期被列为标题。
我还在JSON方法中添加了print语句,以查看是否将对象添加到其中。事实证明,该对象已添加到列表中。设置适配器或在JSON中调用适配器的方式可能存在一些问题。不确定
为此,我从这篇文章中获得了帮助 Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date
我尝试了使用伪数据,并且效果很好。但是,当我从JSON文件中读取并尝试在android中采用时,我可能做错了。
我正在做的是parseJSOn方法正在创建对象并将其添加到ArrayList中。并将ArrayList传递到哈希图中?这是做的方式吗?我看过很多关于JSON解析的教程,但是当我的案例标题和事件中有多种视图类型时,并没有真正的想法
此外,有人可以帮助我实现我的案例中的onclick侦听器回收器视图吗? (在大多数情况下,模型都是作为获取职位的参考传递的。那么在我的情况下会是什么?
我已经发布了带有说明的代码。感谢您的帮助
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
这是我的数据模型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;
}
}
答案 0 :(得分:1)
您正在以异步方式获取数据,但是您正在recyclerview中将数据设置为同步方式,这就是列表为空的原因。遵循给定的策略:
将adapter.notifyDataSetChanged()替换为update()方法,并在parseJSON()之后删除所有代码;方法。
private void update(){
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);}