求助,使用recyclerView时,notifyDataSetChanged顺序错乱
已解决
求助

我的adapter里面传入一个list,初始化的时候是对的,当我切换不同日期的数据,从数据库中获取,想要显示在上面时,却乱序了。

数据库中获取的数据没错,我把获取的list覆盖了本来的list,更新时有数据,但是是错乱重复的,不同月份信息还混在了一起

public void notifyListDateDataChanged(List<BillDetail> newBillDetailsList){
    this.mBillDetailsList.clear();
    this.mBillDetailsList.addAll(newBillDetailsList);
    for (int i = 0; i <mBillDetailsList.size(); i++) {
        Log.d(TAG, "notifyListDateDataChanged: 第"+i+":"+mBillDetailsList.get(i).get_id());
    }
    notifyDataSetChanged();
}


上面代码是外部传入list更改数据用的,有没有大神踩过这个坑,求助,是我理解的不对吗

adapter全部代码

public class BillListAdapter extends RecyclerView.Adapter<BillListAdapter.InnerViewHolder> {


    
plaintext
复制代码
private static final String TAG = "BillListAdapter";
private List&lt;BillDetail&gt; mBillDetailsList;
private TextView tvListNote,tvListAmount;
private ImageView tvListSrc;
private TextView tvTotalDate,tvTotalOut,tvTotalIn;
private Table_Bill_Dao mTable_bill_dao;
private final int VIEW_TYPE_BILL = 1;
private final int VIEW_TYPE_NOT_BILL = -1;
private Table_Icon_Dao mTable_icon_dao;

public BillListAdapter(List&lt;BillDetail&gt; billDetailList){
    this.mBillDetailsList = billDetailList;
}
@NonNull
@Override
public BillListAdapter.InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;
    mTable_bill_dao = new Table_Bill_Dao(parent.getContext());
    mTable_icon_dao = new Table_Icon_Dao(parent.getContext());
    if(viewType == VIEW_TYPE_BILL){//每个数据项
        view = View.inflate(parent.getContext(), R.layout.detail_list_layout_per,null);
        Log.d(TAG, "onCreateViewHolder: in"+VIEW_TYPE_BILL);
    }
    else{//统计数据项
        view = View.inflate(parent.getContext(), R.layout.detail_list_layout_count,null);
        Log.d(TAG, "onCreateViewHolder: in"+VIEW_TYPE_NOT_BILL);
    }
    return new InnerViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull BillListAdapter.InnerViewHolder holder, int position) {
    Log.d(TAG, "onBindViewHolder: 现在的position是"+position);
    if(mBillDetailsList.get(position).get_id()!=-1){
        holder.setData(mBillDetailsList.get(position));
        holder.itemView.setTag(String.valueOf(mBillDetailsList.get(position).get_id())+mBillDetailsList.get(position).get_date());
        Log.d(TAG, "onBindViewHolder: 数据加载"+holder.getAdapterPosition()+" "
                +mBillDetailsList.get(holder.getAdapterPosition()).get_id());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 点到了第"+holder.getAdapterPosition()+"项,库里的id是");

            }
        });
    }else{
        holder.setTotalData(mBillDetailsList.get(position));
    }
}

@Override
public int getItemCount() {
    return mBillDetailsList.size();
}

@Override
public int getItemViewType(int position) {
    if(mBillDetailsList.get(position).get_id()==-1){//代表不是账单项
        return VIEW_TYPE_NOT_BILL;
    }else return VIEW_TYPE_BILL;

}

@Override
public long getItemId(int position) {
    return position;
}



public void notifyListDateDataChanged(List&lt;BillDetail&gt; newBillDetailsList){//外部更新list
    this.mBillDetailsList.clear();
    notifyItemRangeRemoved(0,mBillDetailsList.size());
    this.mBillDetailsList.addAll(newBillDetailsList);
    for (int i = 0; i &lt;mBillDetailsList.size(); i++) {
        Log.d(TAG, "notifyListDateDataChanged: 第"+i+":"+mBillDetailsList.get(i).get_id());
    }
    notifyDataSetChanged();
}

public class InnerViewHolder extends RecyclerView.ViewHolder{
    public InnerViewHolder(@NonNull View itemView) {//根据布局类型初始化
        super(itemView);
        if(itemView.findViewById(R.id.detail_list_notes)!=null){
            tvListNote = itemView.findViewById(R.id.detail_list_notes);
            tvListAmount = itemView.findViewById(R.id.detail_list_amount);
            tvListSrc = itemView.findViewById(R.id.detail_list_src);
        }
        if(itemView.findViewById(R.id.detail_total_date)!=null){
            tvTotalDate = itemView.findViewById(R.id.detail_total_date);
            tvTotalOut = itemView.findViewById(R.id.detail_total_count_outgo);
            tvTotalIn = itemView.findViewById(R.id.detail_total_count_income);
        }

    }

    public void setData(BillDetail billDetail) {//设置数据
        CategoryIcon categoryIcon =  mTable_icon_dao.getIconById(billDetail.getIcon_id());
        Log.d(TAG, "setData: 现在加载的是"+billDetail.get_id());
        int resourceId = itemView.getResources().getIdentifier(categoryIcon.getSrc(),"drawable",itemView.getContext().getPackageName());
        if(Constants.OUTGO_STRING.equals(categoryIcon.getType())){
            tvListAmount.setText("-"+billDetail.getAmount());
        }else{
            tvListAmount.setText(billDetail.getAmount());
        }
        if(billDetail.getNotes()==null||billDetail.getNotes().length()&lt;1){
            tvListNote.setText(categoryIcon.getDescription());
        }else{
            tvListNote.setText(billDetail.getNotes());
        }
        if(resourceId!=0){
            tvListSrc.setImageDrawable(ContextCompat.getDrawable(itemView.getContext(),resourceId));
        }
    }

    public void setTotalData(BillDetail billDetail) {//设置数据项数据
        String inPerDay = mTable_bill_dao.countInPerDay(billDetail.get_date());
        String outPerDay = mTable_bill_dao.countOutPerDay(billDetail.get_date());
        Log.d(TAG, "setTotalData: 现在加载的是"+billDetail.get_id());
        tvTotalDate.setText(billDetail.get_date());
        if(outPerDay==null)
            tvTotalOut.setText("0");
        else
            tvTotalOut.setText(outPerDay);
        if(inPerDay==null)
            tvTotalIn.setText("0");
        else
            tvTotalIn.setText(inPerDay);
    }
}

}

编辑器加载中...
3个回答