fire_head fire_head - 2 months ago 22
JSON Question

recycler view not displaying retrofit json result

I am trying to get a JSON response using retrofit in a fragment, I'm getting this message "No adapter attached; skipping layout" however i don't think the problem is there. the onResponse is not being called.. so I believe the problem is:

getUserCalendar = ((BaseActivity)getActivity()).getApi().calendar(id,email);


BaseActivity is not the fragment's activity but its extended by the fragment's activity. Which is where the getApi method is. getCalendar is definitely being called just not onResponse. So the problem is that the recycler view is not displaying the results.

//--------JSON response--------
{
"CalendarList": [
{
"id": 1,
"name": "asdasd",
"description": "asdasd"
},
{
//etc
}
]
}

,

//--------EventObject (model)--------

public void setEventName (String event_name) {
this.event_name = event_name;
}

public String getEventName() {
return event_name;
}

//etc

,

//--------API--------


@FormUrlEncoded
@POST ("api/calendar.php")
Call<List<EventObject>> calendar(@Field("id") String Id,@Field("email") String email);

,

//--------Calendar fragmnet/class--------

ArrayList<EventObject > calendarEvents = new ArrayList<EventObject >();
RecyclerView recyclerCalendarView;
private Call<List<EventObject >> getUserCalendar;

//in the onCreateView method
{
recyclerCalendarView = (RecyclerView) calendarFragmentView.findViewById(R.id.list_calendar_events);
recyclerCalendarView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

getCalendar();
}

private void getCalendar() {
getUserCalendar = ((BaseActivity)getActivity()).getApi().calendar(id,email);
getUserCalendar.enqueue(new Callback<List<EventObject >>() {
@Override
public void onResponse(Call<List<EventObject >> call, Response<List<EventObject >> response) {
if (getActivity().isFinishing()) {
return;
}

if (response.isSuccessful()) {
List<EventObject > eventList;
eventList = new ArrayList<EventObject >();

EventObject tempResult= new EventObject ();

for (int i = 0; i < eventList.size(); i++) {
tempResult = new EventObject ();

tempResult.setEventName(eventList.get(i).getEventName());
tempResult.setEventDescription(eventList.get(i).getEventDescription());
tempResult.setEventAuthorFirstName(eventList.get(i).getEventAuthorFirstName());
tempResult.setEventAuthorLastName(eventList.get(i).getEventAuthorLastName());
tempResult.setEventAuthorId(eventList.get(i).getEventAuthorId());

calendarEvents.add(tempResult);
}

CalendarAdapter adapter = new CalendarAdapter(getContext(), calendarEvents);
recyclerCalendarView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getContext(), R.string.something_wrong_calendar, Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call<List<EventObject >> call, Throwable t) {
Log.i("app", "FAILURE:" + t.getMessage());
}
});
}

,

//--------CalendarAdapter Class inside the fragment class--------

class CalendarAdapter extends RecyclerView.Adapter<CalendarRecyclerViewHolder> {
public ArrayList<EventObject > eventCalendar = new ArrayList<EventObject >();
private Context context;
int count;

public CalendarAdapter(Context context, ArrayList<EventObject > event_calendar) {
this.eventCalendar = event_calendar;
this.count = event_calendar.size();
this.context = context;
}

@Override
public CalendarRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.event_participants_test, parent, false);
CalendarRecyclerViewHolder listHolder = new CalendarRecyclerViewHolder(mainGroup);

return listHolder;
}

private void Add(String id) {

}

@Override
public void onBindViewHolder(final CalendarRecyclerViewHolder holder, int position) {
final EventObject object = eventCalendar.get(position);

final CalendarRecyclerViewHolder mainHolder = (CalendarRecyclerViewHolder) holder;

mainHolder.txt_calendar_name.setText(object.getEventName());
mainHolder.txt_calendar_description.setText(object.getEventDescription());
//etc
}
}

@Override
public int getItemCount() {
return (null != eventCalendar ? eventCalendar.size() : 0);
}
}



//--------CalendarRecyclerViewHolder--------

public class CalendarRecyclerViewHolder extends RecyclerView.ViewHolder {

public ImageView img_calendar_author;
public TextView txt_calendar_name;
public TextView txt_calendar_author;
public TextView txt_calendar_description;

public CalendarRecyclerViewHolder(View view) {
super(view);

this.img_calendar_author = (ImageView) view.findViewById(R.id.img_calendar_author);
this.txt_calendar_name = (TextView) view.findViewById(R.id.txt_calendar_name);
this.txt_calendar_author = (TextView) view.findViewById(R.id.txt_calendar_author);
this.txt_calendar_description = (TextView) view.findViewById(R.id.txt_calendar_description);
}

Answer
if (response.isSuccessful()) {
                //Check this
                List<EventObject > eventList;
                eventList = new ArrayList<EventObject >();

                //It should be something like
                //eventList = response.body();

                EventObject tempResult= new EventObject ();

                for (int i = 0; i < eventList.size(); i++) {
                    tempResult = new EventObject ();

                    tempResult.setEventName(eventList.get(i).getEventName());
                    tempResult.setEventDescription(eventList.get(i).getEventDescription());
                    tempResult.setEventAuthorFirstName(eventList.get(i).getEventAuthorFirstName());
                    tempResult.setEventAuthorLastName(eventList.get(i).getEventAuthorLastName());
                    tempResult.setEventAuthorId(eventList.get(i).getEventAuthorId());

                    calendarEvents.add(tempResult);
                }

                CalendarAdapter adapter = new CalendarAdapter(getContext(), calendarEvents);
                recyclerCalendarView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            } else {
                Toast.makeText(getContext(), R.string.something_wrong_calendar, Toast.LENGTH_LONG).show();
            }