我有一个这样的课程:
public class ComponentMain implements Serializable {
private ArrayList<Integer> componentsIDs;
private int availableCount;
private String component_name;
private String component_desc;
我正在使用RecycleView。
在活动中代码看起来像这样
private List<ComponentMain> componentMains = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mAdapter = new ComponentAdapter(getActivity(),componentMains);
recyclerView.setAdapter(mAdapter);
我正在向componentMains添加元素。
private List<Component> componentList = new ArrayList<>();
//adding data to componentList here.
String tmp = componentList.get(0).getComponent_name() ;
ArrayList<Integer> oneComponent = new ArrayList<>();
oneComponent.add(componentList.get(0).getComponent_id_Integer());
int i;
for (i = 1; i< componentList.size(); ++i) {
if(componentList.get(i).getComponent_name().equals(tmp)) {
oneComponent.add(componentList.get(i).getComponent_id_Integer());
tmp = componentList.get(i).getComponent_name();
} else {
//ArrayList<Integer> componentsIDs, String component_name, String component_desc
//
ComponentMain mainComp = new ComponentMain(oneComponent,componentList.get(i-1).getComponent_name(),componentList.get(i-1).getComponent_desc());
componentMains.add(mainComp);
oneComponent.clear();
oneComponent.add(componentList.get(i).getComponent_id_Integer());
tmp = componentList.get(i).getComponent_name();
}
}
ComponentMain mainComp = new ComponentMain(oneComponent,componentList.get(i-1).getComponent_name(),componentList.get(i-1).getComponent_desc());
componentMains.add(mainComp);
}
我按名称对组件进行分组,并为每个名称存储ID的ArrayList; 最后我打电话给
mAdapter.notifyDataSetChanged();
问题是在我致电 notifyDataSetChanged()之后,对于 componentMains列表中的每个对象, componentsIDs list 的值与componentsMains列表中最后一个的值。
这里是代码od ComponentAdapter类
public class ComponentAdapter extends RecyclerView.Adapter<ComponentAdapter.MyViewHolder> {
private List<ComponentMain> componentList;
private Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name,description,availableCount;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
description = (TextView) view.findViewById(R.id.description);
available = (TextView) view.findViewById(R.id.available);
}
}
public ComponentAdapter(Context context, List<ComponentMain> componentList) {
this.context = context;
this.componentList = componentList;
}
@Override
public ComponentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.component_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ComponentAdapter.MyViewHolder holder, int position) {
ComponentMain component = componentList.get(position);
holder.name.setText(component.getComponent_name());
holder.description.setText(component.getComponent_desc());
holder.available.setText(component.getAvailableCount()+"");
}
@Override
public int getItemCount() {
return componentList.size();
}
}