【发布时间】:2016-02-03 21:13:04
【问题描述】:
请注意,我已经四处搜索并尝试以不同的方式实现它。但我需要一些建议。
我的 Java 类中有以下 Map(已注释掉静态项)
/**
* An array of YouTube videos
*/
public static List<YouTubeVideo> ITEMS = new ArrayList<>();
/**
* A map of YouTube videos, by ID.
*/
public static Map<String, YouTubeVideo> ITEM_MAP = new HashMap<>();
/*
static {
addItem(new YouTubeVideo("x-hH_Txxzls", "Titlee 2"));
addItem(new YouTubeVideo("TTh_qYMzSZk", "Title 3"));
addItem(new YouTubeVideo("tttG6SdnCd4", "Some Title"));
addItem(new YouTubeVideo("x-hH_Txxzls", "This post title"));
addItem(new YouTubeVideo("TTh_qYMzSZk", "Post title"));
addItem(new YouTubeVideo("tttG6SdnCd4", "Titles"));
addItem(new YouTubeVideo("x-hH_Txxzls", "again titles"));
}*/
private void addItem(final YouTubeVideo item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A POJO representing a YouTube video
*/
public static class YouTubeVideo {
public String id;
public String title;
public YouTubeVideo(String id, String content) {
this.id = id;
this.title = content;
}
@Override
public String toString() {
return title;
}
}
这是我的解析器(现在它将数据发送到另一个列表并且它运行良好)
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
IndividualPosts IndividualPost = new IndividualPosts();
JSONObject json = null;
try {
json = array.getJSONObject(i);
IndividualPost.setTitleIn(json.getString(Config.TAG_TITLE));
IndividualPost.setLink(json.getString(Config.TAG_VIDEO_ID));
} catch (JSONException e) {
e.printStackTrace();
}
listIndividualPosts.add(IndividualPost);
}
//Finally initializing our adapter
adapter = new CardAdapter(listIndividualPosts, getActivity());
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
但是我不知道如何将数据发送到这个 hashMap,就像我有静态项目一样:(
主要问题是 HashMap 应该有一个可以更新的项目列表。 基本上,它会创建一个包含标题和视频缩略图的帖子列表。
任何建议将不胜感激。干杯。
【问题讨论】:
标签: java android json android-volley