卡片视图未出现在 RecyclerView 中

Card View not appearing in RecyclerView

我正在构建一个连接到 REST API 并检索数据库中所有员工的应用程序。该应用程序使用 RecyclerView 显示在 CardView 中带回的所有员工。问题是 RecyclerView 中什么也没有出现,并且有数据从 REST API.

返回

这是一个 Android 应用程序,内置于 Java,使用 Pie 作为 OS。该项目是使用基本 Activity 模板创建的。

我的ActivityClass

public class ViewAllEmployees extends AppCompatActivity {

    private List<Employee> employees;
    private RecyclerView rcView;
    private EmployeeAdapter adapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_employees);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        context = this;

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FetchEmployeesTask async = new FetchEmployeesTask(new AsyncResponse() {
            @Override
            public void processFinished(List<Employee> output) {
                Log.i("Async", "Finished");
            }
        });

        async.execute();

        rcView = findViewById(R.id.rcView);
        adapter = new EmployeeAdapter(context, employees);
        rcView.setAdapter(adapter);
        rcView.setLayoutManager(new LinearLayoutManager(context));





    }


    private class FetchEmployeesTask extends AsyncTask<Void, Void, List<Employee>> {


        public AsyncResponse delegate = null;

        public  FetchEmployeesTask(AsyncResponse asyncResponse){
            delegate = asyncResponse;
        }


        @Override
        protected List<Employee> doInBackground(Void... voids) {

            return new APIHelper().fetchItems();
        }

        @Override
        protected void onPostExecute(List<Employee> items) {
            employees = items;
            delegate.processFinished(items);






        }
    }

    public interface AsyncResponse{
        void processFinished(List<Employee> output);
    }






}

RecyclerView 的员工适配器

public class EmployeeAdapter extends RecyclerView.Adapter<EmployeeAdapter.EmployeeViewHolder> {


    private List<Employee> employees;
    private final LayoutInflater mInflater;
    private Context context;
    public int position;

    public EmployeeAdapter(Context context, List<Employee> employeeList){
        this.context = context;
        employees = new ArrayList<>();
        mInflater = LayoutInflater.from(context);
        employees = employeeList;
    }

    @NonNull
    @Override
    public EmployeeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mItemView = mInflater.inflate(R.layout.employee_card, viewGroup, false);
        return new EmployeeViewHolder(mItemView, this);
    }

    @Override
    public void onBindViewHolder(@NonNull EmployeeViewHolder employeeViewHolder, int i) {
        Employee empl = employees.get(i);
        employeeViewHolder.name.setText(empl.firstName + empl.lastName);
        employeeViewHolder.department.setText(empl.department);
    }

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

    class EmployeeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public final TextView name;
        public final TextView department;
        private EmployeeAdapter adapter;
        private final CardView card;

        public EmployeeViewHolder(View itemView, EmployeeAdapter adapter) {
            super(itemView);
            name = itemView.findViewById(R.id.txtName);
            department = itemView.findViewById(R.id.txtDepartment);
            card = itemView.findViewById(R.id.cv);
            itemView.setOnClickListener(this);
            this.adapter = adapter;
        }

        @Override
        public void onClick(View view) {

        }
    }



}

API帮手class

public class APIHelper {

    private static final String ENDPOINT = "ENDPOINTADDRESS";
    private static final String TAG = "APIHelper";

    public byte[] getUrlBytes(String urlSpec) throws IOException{
        URL url = new URL(urlSpec);

        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        try{
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream in = connection.getInputStream();

            if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
                return null;
            }

            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while((bytesRead = in.read(buffer)) > 0){
                out.write(buffer, 0, bytesRead);
            }
            out.close();
            return out.toByteArray();
        }
        finally {
            connection.disconnect();
        }
    }

    public String getUrlString(String urlSpec) throws IOException{
        return new String((getUrlBytes(urlSpec)));
    }

    private void parseItems(List<Employee> employees, JSONArray jsonBody) throws IOException, JSONException {

        JSONArray employeeArray = jsonBody;

        for(int i = 0; i < employeeArray.length(); i++){
            JSONObject jsonEmployee = employeeArray.getJSONObject(i);
            JSONObject job = jsonEmployee.getJSONObject("Job");

            Employee emp = new Employee();
            //emp.setDepartment(jsonEmployee.getString("DepartmentId"));
            emp.setEmail(jsonEmployee.getString("EmailAddress"));
            emp.setEmployeeID(jsonEmployee.getString("EmployeeID"));
            emp.setFirstName(jsonEmployee.getString("FirstName"));
            emp.setLastName(jsonEmployee.getString("LastName"));
            emp.setMiddleInitial(jsonEmployee.getString("MiddleInitial"));
            emp.setPosition(job.getString("JobTitle"));
            emp.setWorkNumber(jsonEmployee.getString("WorkNumber"));


            employees.add(emp);

        }
    }

    public List<Employee> fetchItems(){

        List<Employee> employees = new ArrayList<>();

        try{
            String url =  Uri.parse(ENDPOINT).buildUpon().build().toString();

            String jsonString = getUrlString(url);
            Log.i(TAG, "Received JSON: " + jsonString);
            JSONArray jsonBody = new JSONArray(jsonString);
            parseItems(employees, jsonBody);
        }
        catch (IOException ioe){
            Log.e(TAG, "Failed to fetch items", ioe);
        }
        catch (JSONException je){
            Log.e(TAG, "Failed to parse json:", je);
        }

        return employees;
    }

ViewAllEmployees 的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ViewAllEmployees"
    tools:showIn="@layout/activity_view_all_employees">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcView"
        android:layout_width="395dp"
        android:layout_height="659dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Recyclerview 中的卡片视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="16dp">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:cardElevation="5dp">



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <TextView
                android:id="@+id/txtName"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Name"/>
            <TextView
                android:id="@+id/txtDepartment"
                android:layout_width="match_parent"
                android:layout_height="127dp"
                android:layout_below="@+id/txtName"
                android:layout_alignEnd="@+id/txtName"
                android:layout_marginTop="-142dp"
                android:layout_marginEnd="0dp" />





        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

当应用程序为 运行 时,RecyclerView 应填充显示员工姓名及其部门的 CardView。但是,它目前什么都不显示。

您必须在填充数组列表后通知您的适配器。初始化您的员工数组列表变量,然后执行:

private class FetchEmployeesTask extends AsyncTask<Void, Void, List<Employee>> {


    public AsyncResponse delegate = null;

    public  FetchEmployeesTask(AsyncResponse asyncResponse){
        delegate = asyncResponse;
    }


    @Override
    protected List<Employee> doInBackground(Void... voids) {

        return new APIHelper().fetchItems();
    }

    @Override
    protected void onPostExecute(List<Employee> items) {
        employees.addAll(items);
        adapter.notifyDataSetChanged();
        delegate.processFinished(items);

    }
}
FetchEmployeesTask async = new FetchEmployeesTask(new AsyncResponse() {
    @Override
    public void processFinished(List<Employee> output) {
        Log.i("Async", "Finished");
        employees.addAll(items);
        adapter.notifyDataSetChanged();
    }
});