Rony R Rony R - 8 months ago 55
JSON Question

retrofit JSON array to recyclerview

i have JSON response like this

[{"id":94973,"slug":"la-liga-real-betis-1-1-barcelona-29-januari-2017","type":"post"},{"id":94957,"slug":"fa-chelsea-4-0-brentford-28-januari-2017","type":"post"},{"id":94944,"slug":"serie-a-ac-milan-1-2-napoli-22-januari-2017","type":"post"},{"id":94663,"slug":"boullier-soroti-performa-mclaren-musim-ini","type":"post"},{"id":94660,"slug":"balotelli-tawarkan-diri-ke-sassuolo","type":"post"}]


and i want to pass this with retrofit
i already check there is nothing wrong with get data from json, but my recyclerview doesnt show any data.i think i was wrong at pass the response to the arraylist then adapter.

my on response class

public class EditorFragment extends Fragment {

private RecyclerView recyclerView;
private ArrayList<Coba> data;
//private List<Coba> tes;
private MyAdapter adapter;


public EditorFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadJSON();
}


private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.opinianda.com/wp-json/wp/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestSumbol request = retrofit.create(RequestSumbol.class);
Call<List<Coba>> call = request.getPostDet();
call.enqueue(new Callback<List<Coba>>() {
@Override
public void onResponse(Call<List<Coba>> call, Response<List<Coba>> response) {
try {
List<Coba> jsonResponse = response.body();
data = new ArrayList<>(jsonResponse);
Log.i("HASIL", "onResponse: "+data);
}catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}

@Override
public void onFailure(Call<List<Coba>> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
recyclerView.setHasFixedSize(true);
// loadJSON();
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);

return rootView;
}


}

and this onbindview in my adapter class

public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
holder.mTextView.setText(coba.get(position).getId());
holder.mDetView.setText(coba.get(position).getSlug());
holder.mVerView.setText(coba.get(position).getType());
holder.mCardView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(),DetailBerita.class);
v.getContext().startActivity(intent);
System.out.println("Clicked");
}
});
}

Answer Source

Looks like you need to notify the adapter. Retrofit won't do that for you.

Also, don't set data = new ArrayList. You need to instead clear() and addAll() if you want it to display in the adapter.

request.getPostDet().enqueue(new Callback<List<Coba>>() {
    @Override
    public void onResponse(Call<List<Coba>> call, Response<List<Coba>> response) { 
        data.clear();
        try {
            List<Coba> responseBody = response.body();
            data.addAll(responseBody);
            Log.i("HASIL", "onResponse: "+data);
        } catch (Exception e) {
            Log.d("onResponse", "There is an error");
            e.printStackTrace();
        }
        adapter.notifyDataSetChanged();
    }