【发布时间】:2025-12-12 18:35:01
【问题描述】:
enter image description here这就是我在苏丹马哈茂德帮助下得到的(他在下面的评论中发布)。但是从这里发布的代码中,我得到了空白布局 这就是我要的 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;
}
}
【问题讨论】:
-
从响应中获取 arraylist 后尝试设置回收器适配器..
-
我也这样做了,但布局中仍然显示空白
-
调试并检查您传递给回收器适配器的数组列表中有数据。
-
我在调试中发现有趣的是,当我在它打印数据的方法中打印 arraylist 时,但是当我在调用 parseJSON 方法后打印它时,它什么也不打印
-
当我在这行 parseJson() 之后打印时,它什么也不打印