如何从List <mylist> Array列表中获取单个String项

时间:2017-12-18 11:13:01

标签: android json

我正在使用recyclerview我正在以json格式从API获取数据。所以我创建了两个模型。一个是我的回收者查看数据,另一个是我的日历视图。我使用自定义适配器成功地在回收站视图中获取数据。现在我想在第二个模型中从json响应中获取日期。我这样做了。

我如何从JSON响应中读取日期:

final List<MyEventsDateModel> dateModel = new ArrayList<>();
String date = jsonArray.getJSONObject(i).getJSONObject("eventStudentListIbfk2rel").optString("scheduledDate");
             String mainDate[] = date.split("T");
             String eventDate = mainDate[0];
             dateModel.add(new MyEventsDateModel(eventDate));
             callback.onSuccess(dateModel);

MyEventsDateModel.Class

public class MyEventsDateModel extends BaseModel {
private String myEventDate;

public MyEventsDateModel(String myEventDate) {
    this.myEventDate = myEventDate;
}

public MyEventsDateModel(Context actContext, List<MyEventsResponseModel> myEventsModel) {
}

public String getMyEventDate() {
    return myEventDate;
}

public void setMyEventDate(String myEventDate) {
    this.myEventDate = myEventDate;
}
}

现在在成功方法中我有我的日期模型,我喜欢这样使用:

public void onSuccess(List<MyEventsDateModel> dateModel) {
//NOW HERE I WANT TO GET getMyEventDate(); how can i do that?
        }

尝试过这样,但它说的是nullPointerException

 String myDate = myEventsDateModel.getMyEventDate();
    LoggerUtils.error("Date", "" + myDate);
    addDate(myDate);

&lt;&lt; - 为什么这个问题被标记为重复?我知道什么是空指针异常。我询问了Json解析并从for循环中获取响应中的每个字符串。请删除重复的标记 - &gt;&gt;

1 个答案:

答案 0 :(得分:1)

试试这样:

MyEventsDateModel myEventsDateModel = dateModel.get(<PASS YOUR INDEX HERE>)
String myDate = myEventsDateModel.getMyEventDate();
LoggerUtils.error("Date", "" + myDate);
addDate(myDate);