【问题标题】:Card View not appearing in RecyclerView卡片视图未出现在 RecyclerView 中
【发布时间】:2019-05-07 16:03:17
【问题描述】:

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

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

我的活动课

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

<?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。但是,它目前什么也不显示。

【问题讨论】:

    标签: java android android-recyclerview android-cardview


    【解决方案1】:

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

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

    【讨论】:

    • 添加了通知适配器的代码,但仍然没有填充 RecyclerView
    • 您是否在您的 api 响应中获取列表。如果是的话,你可以添加你的 lisas 我在上面的答案中所做的更改
    • 数据被正确检索,因为我让它将 JSON 字符串记录到日志中。我已将 APIHelper 类的代码添加到帖子中
    • 如果您在 api 响应中获取员工列表,请尝试我添加到答案中的编辑
    【解决方案2】:
    FetchEmployeesTask async = new FetchEmployeesTask(new AsyncResponse() {
        @Override
        public void processFinished(List<Employee> output) {
            Log.i("Async", "Finished");
            employees.addAll(items);
            adapter.notifyDataSetChanged();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2020-08-18
      相关资源
      最近更新 更多