[英]Json text to RecyclerView in Android
I need help with my RecyclerView. 我的RecyclerView需要帮助。 I have a atring array from my SQL Database which looks like this: 我的SQL数据库中有一个atring数组,如下所示:
{"success":true,"0":{"order_number":"078","typ_first":"E3rft","typ_last":"Split","order_date_time":"2016-10-11 19:20:03"},"1":{"order_number":"166","typ_first":"E483f","typ_last":"Split_test","order_date_time":"2016-10-12 18:39:30"}}
In my RecyclerView I have the following fields: 在我的RecyclerView中,我具有以下字段:
order_number
typ_all
(type first and last) typ_all
(第一个和最后一个类型) date
(only a date without time) date
(只有一个没有时间的日期) This is how I get my string array: 这是我获取字符串数组的方式:
String plansData = plansPreferenceData.getString("plansPreferenceData", "");
This is how I set the data to my RecyclerView: 这是我将数据设置到RecyclerView的方式:
// Set plan data
Plans plan = new Plans("123", "E3rft Split", "11.10.2016");
// Add Object to list
planList.add(plan);
// Notify data changes
pAdapter.notifyDataSetChanged();
My Plans
class: 我的Plans
课程:
public class Plans {
private String planTitle, planType, planDate;
public Plans(String planTitle, String planType, String planDate) {
this.planTitle = planTitle;
this.planType = planType;
this.planDate = planDate;
}
public void setPlanTitle(String planTitle) {
this.planTitle = planTitle;
}
public String getPlanTitle() {
return planTitle;
}
public void setPlanType(String planType) {
this.planType = planType;
}
public String getPlanType() {
return planType;
}
public void setPlanDate(String planDate) {
this.planDate = planDate;
}
public String getPlanDate() {
return planDate;
}
}
My onCreateView
: 我的onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_training, container, false);
preparePlansData();
return view;
}
My preparePlansData()
: 我的preparePlansData()
:
private void preparePlansData() {
// Set plan data
Plans plan = new Plans("123", "fkfjfjeje", "21.04.1977");
// Add Object to list
planList.add(plan);
plan = new Plans("test", "tttt", "22.01.2017");
planList.add(plan);
// Notify data changes
pAdapter.notifyDataSetChanged();
}
My question is how can I get the information out of the string array into my adapter? 我的问题是如何从字符串数组中获取信息到适配器? I also need to format the date before adding. 添加之前,我还需要格式化日期。 Thanks for your help! 谢谢你的帮助!
read about Gson here: 在这里阅读有关Gson的信息:
http://guides.codepath.com/android/leveraging-the-gson-library http://guides.codepath.com/android/leveraging-the-gson-library
After that you will be able to write code like that: 之后,您将可以编写如下代码:
Type type = new TypeToken<Map<Integer, Plans>>(){}.getType();
Map<Integer, Plans> myMap = gson.fromJson("your json from db", type);
and use this map.values() in your adapter 并在您的适配器中使用此map.values()
your Plans
class should look like this: 您的Plans
类应如下所示:
class Plans {
String order_number;
String typ_first;
String typ_last;
String order_date_time;
}
If you want another field names you have to use @SerializedName
annotation 如果需要其他字段名称,则必须使用@SerializedName
批注
Finally, you should write something like that, (I do not know if syntax is 100% do not have IDE open now) : 最后,您应该编写类似的内容((我不知道语法是否为100%,现在不打开IDE):
private void preparePlansData() {
String plansData = plansPreferenceData.getString("plansPreferenceData", "");
Type type = new TypeToken<Map<Integer, Plans>>(){}.getType();
Map<Integer, Plans> myMap = gson.fromJson(plansData, type);
planList.addAll(myMap.values());
// Notify data changes
pAdapter.notifyDataSetChanged();
}
and modify your model class: 并修改您的模型类:
public class Plans {
@SerializedName("order_number")
String planTitle;
@SerializedName("typ_last")
String planType;
@SerializedName("order_date_time")
String planDate;
....
I hope it will help you. 希望对您有帮助。
Check this code (you can remove @Test annotation): 检查以下代码(您可以删除@Test批注):
class Plans {
String typ_firstString;
String typ_last;
String order_date_time;
public Plans(String typ_firstString, String typ_last, String order_date_time) {
this.typ_firstString = typ_firstString;
this.typ_last = typ_last;
this.order_date_time = order_date_time;
}
}
class PlansResponse {
boolean status;
List<Plans> plans;
}
@Test
public void testPlacesResponseDeserializer() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(PlansResponse.class, new PlansResponseDeserializer())
.create();
String jsonString = "{\"success\":true,\"0\":{\"order_number\":\"078\",\"typ_first\":\"E3rft\",\"typ_last\":\"Split\",\"order_date_time\":\"2016-10-11 19:20:03\"},\"1\":{\"order_number\":\"166\",\"typ_first\":\"E483f\",\"typ_last\":\"Split_test\",\"order_date_time\":\"2016-10-12 18:39:30\"}}";
PlansResponse plansResponse = gson.fromJson(jsonString, PlansResponse.class);
assert plansResponse.status == true;
assert plansResponse.plans.size() == 2;
}
class PlansResponseDeserializer implements JsonDeserializer<PlansResponse> {
private String getElementAsString(JsonObject jsonObject, String key) {
JsonElement element = jsonObject.get(key);
return element.getAsString();
}
@Override
public PlansResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
PlansResponse plansResponse = new PlansResponse();
List<Plans> plansList = new ArrayList<>();
Gson gson = new Gson();
Type type = new TypeToken<Map<String, JsonObject>>(){}.getType();
Map<String, JsonElement> map = gson.fromJson(json, type);
for(Map.Entry<String, JsonElement> entry : map.entrySet()) {
String key = entry.getKey();
if("success".equals(key)) {
JsonPrimitive jsonPrimitive = (JsonPrimitive) entry.getValue();
plansResponse.status = jsonPrimitive.getAsBoolean();
continue;
}
JsonObject jsonObject = (JsonObject)entry.getValue();
String typ_firstString = getElementAsString(jsonObject, "typ_first");
String typ_last = getElementAsString(jsonObject, "typ_last");
String order_date_time = getElementAsString(jsonObject, "order_date_time");
plansList.add(new Plans(typ_firstString, typ_last, order_date_time));
}
plansResponse.plans = plansList;
return plansResponse;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.