java - 卡片 View 未出现在 RecyclerView 中

标签 java android android-recyclerview android-cardview

我正在构建一个连接到 REST API 并检索数据库中所有员工的应用程序。该应用程序使用 RecyclerView 来显示 CardView 中带回的所有员工。问题是 RecyclerView 中没有显示任何内容,并且有从 REST API 返回的数据。

它是一个 Android 应用程序,用 Java 构建,使用 Pie 作为操作系统。该项目是使用基本 Activity 模板创建的。

我的 Activity 课

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) {

        }
    }



}

APIHelper 类

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 中的卡片 View

<?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);

    }
}

关于java - 卡片 View 未出现在 RecyclerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026506/

相关文章:

java - 在 DDMS View 中找不到 CSV 文件?

android - Activity.recreate() 复制列表

android - BottomSheetDialogFragment 内部的 RecyclerView (wrap_content)

java - 构建 HighCharts-Export Server 2.1 失败

java - Java 中的 "java.lang.ArrayIndexOutOfBoundsException"错误

java - 将 Perl 转换为 Java

android - 如何在recyclerview中请求Focus editText?

java - 在 Java 中使用资源文件

android - 如何在自定义 View 上使用 View 绑定(bind)

android - 碰撞时摧毁 body JBox2D android