我正在使用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>
答案 0 :(得分:2)
在for循环之前使用此代码
List<String> att = new ArrayList<>();
答案 1 :(得分:0)
您当前的onBindViewHolder
仅显示“尝试”列表中的最后一个条目。
我相信您希望显示“尝试”列表,因此您的adapter
实现是错误的,应该使用attemptslist
您的Adapter构造器应类似这样
ArrayList<Attempt> attemptArrayList = new ArrayList<>();
public void TestHistoryAdapter(ResponseObj obj, Context context){
attemptArrayList=obj.getAttempts();
this.context = context;}
您的getItemCount
将返回attemptArrayList.size();
您的onBindViewHolder
应该是这样的
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.binding.dashboardTestHistorySubjectNameTv.setText(attemptArrayList.get(position).getTestName());
Log.e("name", String.valueOf(attemptArrayList.get(position).getTestName()));
holder.binding.textView.setText(Integer.toString(attemptArrayList.get(position).getCurrentAttempt()));
holder.binding.dashboardTestHistoryTestDate.setText(attemptArrayList.get(position).getTestDate());
holder.binding.textView.setText(Integer.toString(attemptArrayList.get(position).getCurrentAttempt()));
holder.binding.dashboardTestHistoryTestDate.setText(attemptArrayList.get(position).getTestDate());
}