在我目前的代码中,我能够将一个值传递给RecyclerView。我不确定我错过了什么,或者我是否必须彻底更改我的代码。
这是我的适配器类:
public class TechSkillsAdapter extends RecyclerView.Adapter<TrainingViewHolder> {
Context c;
ArrayList<String> trainings;
public TechSkillsAdapter(Context c, ArrayList<String> trainings) {
this.c = c;
this.trainings = trainings;
}
@Override
public TrainingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(c).inflate(R.layout.cv_traininglist, parent, false);
return new TrainingViewHolder(v);
}
@Override
public void onBindViewHolder(TrainingViewHolder holder, int position) {
holder.date_txt.setText(trainings.get(position));
}
@Override
public int getItemCount() {
return trainings.size();
}
}
我使用改造检索列表,然后将值传递给将其显示到recyclerview的方法。
以下是我如何检索培训:
private void getTraining(final String id, String tid){
com.trendmicro.projectlara.apis.ApiServiceTraining apiService = ApiClient.getClient().create(ApiServiceTraining.class);
Call<List<Training>> call = apiService.getMyTraining(id, tid);
call.enqueue(new Callback<List<Training>>() {
@Override
public void onResponse (Call<List<Training>> call, Response<List<Training>> response) {
List<Training> training = response.body();
for(Training t: training){
fillTrainings(t.getTraining_title().toString(), t.getTraining_date().toString());
}
}
这是我的fillTrainings方法。
我传递了两个值,但我只能显示一个值:
private void fillTrainings(String title, String date) {
training.add(title);
adapter = new TechSkillsAdapter(getContext(), training);
rv.setAdapter(adapter);
progressDialog.dismiss();
}
以下是我要修复的内容,我不知道如何显示日期,因为我添加值的方式是:training.add(title);
答案 0 :(得分:3)
这是因为每当您拥有包含此代码的项目时,您都会重新创建适配器:
@Override
public void onResponse (Call<List<Training>> call, Response<List<Training>> response) {
List<Training> training = response.body();
for(Training t: training){
// here you're only send one time
fillTrainings(t.getTraining_title().toString(), t.getTraining_date().toString());
}
}
您可以通过将方法更改为以下方法将所有列表发送到您的方法来修复它:
private void fillTrainings(List<Training> trainList) {
for(Training train: trainList) {
training.add(train.getTraining_title().toString());
}
adapter = new TechSkillsAdapter(getContext(), training);
rv.setAdapter(adapter);
progressDialog.dismiss();
}
然后你可以用它:
@Override
public void onResponse (Call<List<Training>> call, Response<List<Training>> response) {
List<Training> training = response.body();
fillTrainings(training);
}
答案 1 :(得分:1)
您收到的List
Training
,但由于某种原因,您的适配器由List
String
支持。那是为什么?
修改您的适配器以保存训练,然后将标题和日期绑定到视图。
public class TechSkillsAdapter extends RecyclerView.Adapter<TrainingViewHolder> {
Context c;
ArrayList<Training> trainings;
public TechSkillsAdapter(Context c, ArrayList<Training> trainings) {
this.c = c;
this.trainings = trainings;
}
@Override
public TrainingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(c).inflate(R.layout.cv_traininglist, parent, false);
return new TrainingViewHolder(v);
}
@Override
public void onBindViewHolder(TrainingViewHolder holder, int position) {
holder.title_txt.setText(trainings.get(position).getTraining_title().toString());
holder.date_txt.setText(trainings.get(position).getTraining_date().toString());
}
@Override
public int getItemCount() {
return trainings.size();
}
}
然后在你的onResponse上做:
@Override
public void onResponse (Call<List<Training>> call, Response<List<Training>> response) {
List<Training> training = response.body();
adapter = new TechSkillsAdapter(getContext(), training);
rv.setAdapter(adapter);
progressDialog.dismiss();
}
这应该可行,但如果您创建并设置适配器一次(在onCreate中)并将其传递给某个列表,然后在onResponse中更新该列表并执行adapter.notifyDataSetChanged()
排序如下:
//Activity
List<Training> mTrainingsList = new ArrayList<Training>();
//onCreate
adapter = new TechSkillsAdapter(getContext(), mTrainingsList);
...
}
@Override
public void onResponse (Call<List<Training>> call, Response<List<Training>> response) {
List<Training> training = response.body();
mTrainingList.clear();
mTrainingList.addAll(training);
adapter.notifyDatasetChanged();
progressDialog.dismiss();
}
编辑:另外,请注意,在您的代码中,您将浏览培训列表,并为每个项目重新创建适配器!试着在将来避免这种情况。