首页 > 解决方案 > 我的 Recyclerview 只显示最后一项

问题描述

recyclerview用来显示值。我得到了改造的回应,但recyclerview它只显示最后一项。它可以工作多个数组,但不能工作多个对象。

功能:

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        attemptslist = testHistoryResponseList.get(position).getAttempts();

    Log.e("size", String.valueOf(attemptslist.size()));
  for(int i = 0 ; i< attemptslist.size(); i++){

       holder.binding.dashboardTestHistorySubjectNameTv.setText(attemptslist.get(i).getTestName());

        Log.e("name", String.valueOf(attemptslist.get(i).getTestName()));

        holder.binding.textView.setText(Integer.toString(attemptslist.get(i).getCurrentAttempt()));

        holder.binding.dashboardTestHistoryTestDate.setText(attemptslist.get(i).getTestDate());

    }
            holder.binding.textView.setText(Integer.toString(attemptslist.get(i).getCurrentAttempt()));

            holder.binding.dashboardTestHistoryTestDate.setText(attemptslist.get(i).getTestDate());

        }
}

我的 api 是多个对象。在这里,我附加了我的 api。它可以工作多个数组,但不能工作多个对象。

{
    "test_dtl": [
        {
            "test_id": 9,
            "attempts": [
                {
                    "schedule_id": 13,
                    "test_id": 9,
                    "test_instance_id": 93,
                    "current_attempt": 2,
                    "End_Time": "27-Jun-18 01:20:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "science test",
                    "test_desc": "science test",
                    "Test_Date": "27-Jun(16:49)",
                    "percent_score": "20.00"
                },
                {
                    "schedule_id": 16,
                    "test_id": 9,
                    "test_instance_id": 110,
                    "current_attempt": 2,
                    "End_Time": "28-Jun-18 03:22:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "science test",
                    "test_desc": "science test",
                    "Test_Date": "28-Jun(13:31)",
                    "percent_score": "40.00"
                },
                {
                    "schedule_id": 18,
                    "test_id": 9,
                    "test_instance_id": 115,
                    "current_attempt": 1,
                    "End_Time": "28-Jun-18 01:49:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "science test",
                    "test_desc": "science test",
                    "Test_Date": "28-Jun(17:07)",
                    "percent_score": "20.00"
                },
                {
                    "schedule_id": 18,
                    "test_id": 9,
                    "test_instance_id": 117,
                    "current_attempt": 2,
                    "End_Time": "28-Jun-18 02:31:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "science test",
                    "test_desc": "science test",
                    "Test_Date": "28-Jun(17:20)",
                    "percent_score": "20.00"
                },
                {
                    "schedule_id": 18,
                    "test_id": 9,
                    "test_instance_id": 119,
                    "current_attempt": 3,
                    "End_Time": "28-Jun-18 02:52:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "science test",
                    "test_desc": "science test",
                    "Test_Date": "28-Jun(18:11)",
                    "percent_score": "20.00"
                }
            ]
        },
        {
            "test_id": 13,
            "attempts": [
                {
                    "schedule_id": 51,
                    "test_id": 13,
                    "test_instance_id": 239,
                    "current_attempt": 1,
                    "End_Time": "14-Jul-18 01:26:am",
                    "org_id": 2,
                    "user_id": 9,
                    "test_name": "Half Yearly Test",
                    "test_desc": "Half Yearly Test",
                    "Test_Date": "14-Jul(10:38)",
                    "percent_score": "31.00"
                }
            ]
        }
    ],
    "timezone": "- HAST "
}

recyclerview .xml 文件recyclerview只显示最后一项。

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".test_history.TestHistoryFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/dashboard_test_history_tv"
            android:layout_width="match_parent"
            android:layout_height="27dp"
            android:gravity="center"
            android:text="Test History" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_dashboard_test_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="1dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/dashboard_test_history_tv"
            app:layout_constraintVertical_bias="1.0" />

    </LinearLayout>

</layout>

活动代码:

RetrofitService retrofitService = RetrofitServiceGenerator.getRetrofitOauthClient(getContext());

    Call<List<TestHistoryResponse>> testresponsecall = retrofitService.getdashBoardTestHistory(sessionManager.getOrgId());

    testresponsecall.enqueue(new Callback<List<TestHistoryResponse>>() {
        @Override
        public void onResponse(Call<List<TestHistoryResponse>> call, Response<List<TestHistoryResponse>> response) {
            if (response.isSuccessful()) {

                TestHistoryAdapter adapter = new TestHistoryAdapter(response.body(), getContext());

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());

                testHistoryBinding.rvDashboardTestHistory.setLayoutManager(linearLayoutManager);
                testHistoryBinding.rvDashboardTestHistory.setAdapter(adapter);

            }
        }

        @Override
        public void onFailure(Call<List<TestHistoryResponse>> call, Throwable t) {

            Log.e(TAG, "onFailure " + t.getCause());
            Log.e(TAG, "onFailure " + t.getMessage());
            Toast.makeText(getContext(), "No Group Scheduled", Toast.LENGTH_LONG).show();
        }
    });

我的 recyclerview 项目 xml 文件:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dp">

                <TextView
                    android:id="@+id/dashboard_test_history_subject_name_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"/>

                <TextView
                    android:id="@+id/dashboard_test_history_test_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:singleLine="true"
                    android:textSize="12sp"/>

            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>

标签: androidandroid-recyclerviewretrofit2

解决方案


在您的 for 循环之前使用此代码

List<String> att = new ArrayList<>();

推荐阅读