将JSON阵列改造为recyclerview

时间:2017-03-12 04:08:30

标签: android arrays json retrofit

我有像这样的JSON响应

[{"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"}]

我希望通过改造传递这个 我已经检查从json获取数据没有任何问题,但我的recyclerview没有显示任何数据。我认为我错误地将响应传递给arraylist然后适配器。

我的回复课

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;
}

}

和我的适配器类中的onbindview

    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
    holder.mTextView.setText(String.valueOf(coba.get(position).getId()));
    holder.mDetView.setText(String.valueOf(coba.get(position).getSlug()));
    holder.mVerView.setText(String.valueOf(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");
        }
    });
}

1 个答案:

答案 0 :(得分:1)

看起来您需要通知适配器。改造不会为你做到这一点。

另外,请勿设置data = new ArrayList。如果您希望它显示在适配器中,则需要clear()addAll()

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();
    }