ReyclerView。警方数据Json - 没有附加适配器,跳过布局

时间:2017-11-25 14:42:05

标签: android json layout android-recyclerview recycler-adapter

大家下午好,我正在尝试从警方数据网站上获取一些数据。

很遗憾,该应用程序没有显示任何数据" E / RecyclerView:没有附加适配器;跳过布局"。

我附加了LatestCrimesActivity,适配器和ListItem类。

感谢您的帮助。

这是LatestCrimesActivity

package com.example.cosmin.crimerate.Latest_crimes_api;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.cosmin.crimerate.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LatestCrimesActivity extends AppCompatActivity {
    private static final String URL_DATA = "https://data.police.uk/api/crimes-street/all-crime?poly=52.268,0.543:52.794,0.238:52.130,0.478&date=2017-01";

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;

    private List<ListItem> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_latest_crimes);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerViewlatest);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        listItems=new ArrayList<>();

        loadRecyclerViewData();


        }

    private void loadRecyclerViewData() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading data...");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        progressDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray("");

                            for (int i=0; i<array.length();i++){
                                JSONObject o = array.getJSONObject(i);

                                ListItem item = new ListItem(
                                        o.getString("category"),
                                        o.getString("location_type"),
                                        o.getString("street"),
                                        o.getString("status")
                                );
                                listItems.add(item);

                            }
                            adapter = new MyAdapter(listItems, getApplicationContext());
                            recyclerView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }


}

适配器:

package com.example.cosmin.crimerate.Latest_crimes_api;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;

import com.example.cosmin.crimerate.R;

import java.util.List;

/**
 * Created by Cosmin on 25/11/2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {



    private List<ListItem> listItems;

    private Context context;

    public MyAdapter(List<ListItem> listItems, LatestCrimesActivity latestCrimesActivity) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);

        holder.textViewCat.setText(listItem.getCategory());
        holder.textViewLoc.setText(listItem.getLocation());
        holder.textViewStreet.setText(listItem.getStreet());
        holder.textViewStatus.setText(listItem.getStatus());


    }

    @Override
    public int getItemCount() {

        return listItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView textViewCat;
        public TextView textViewLoc;
        public TextView textViewStreet;
        public TextView textViewStatus;


        public ViewHolder(View itemView) {

            super(itemView);

            textViewCat = (TextView) itemView.findViewById(R.id.textViewCat);
            textViewLoc = (TextView) itemView.findViewById(R.id.textViewLoc);
            textViewStreet = (TextView) itemView.findViewById(R.id.textViewStreet);
            textViewStatus = (TextView) itemView.findViewById(R.id.textViewStatus);

        }
    }
}

这是ListItem类:

package com.example.cosmin.crimerate.Latest_crimes_api;

/**
 * Created by Cosmin on 25/11/2017.
 */

public class ListItem {

    private String category;
    private String location;
    private String street;
    private String status;

    public ListItem (String category, String location, String street, String status){
        this.category=category;
        this.location=location;
        this.street=street;
        this.status=status;
    }
    public String getCategory (){
        return category;
    }
    public String getLocation (){
        return location;
    }
    public String getStreet (){
        return street;
    }
    public String getStatus (){
        return status;
    }

}

1 个答案:

答案 0 :(得分:0)

试着改变一些线条。在api通话之前首先设置适配器。

    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewlatest);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    listItems=new ArrayList<>();
    adapter = new MyAdapter(listItems, getApplicationContext());
    recyclerView.setAdapter(adapter);

然后当您收到答案时,填写listItems,然后调用适配器对象的notifyDataSetChange()方法。