Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Recyclerview未得到更新_Android_Android Recyclerview - Fatal编程技术网

Android Recyclerview未得到更新

Android Recyclerview未得到更新,android,android-recyclerview,Android,Android Recyclerview,我正在尝试使用notifyDataSetChanged()更新recyclerview,但是recyclerview没有得到更新,我知道在堆栈溢出问题上有很多类似的问题&我尝试了大多数问题,但在我的情况下似乎没有任何效果 第一次当数据进入片段时,我正在为适配器创建一个对象,并将其设置为运行良好的recyclerview,但下一次当适配器已初始化时,我将尝试通知适配器,该适配器将为我提供一个空的recyclerview,即使提供的列表不是空的 请建议 RecyclerView代码 private

我正在尝试使用
notifyDataSetChanged()
更新recyclerview,但是recyclerview没有得到更新,我知道在堆栈溢出问题上有很多类似的问题&我尝试了大多数问题,但在我的情况下似乎没有任何效果

第一次当数据进入片段时,我正在为适配器创建一个对象,并将其设置为运行良好的recyclerview,但下一次当适配器已初始化时,我将尝试通知适配器,该适配器将为我提供一个空的recyclerview,即使提供的列表不是空的

请建议

RecyclerView代码

 private void showStateList(List<State> states) {
        if (states != null) {
            this.lstStates = states;
            if(stateAdapter == null){
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                stateFragmentBinding.stateRecyclerView.setLayoutManager(linearLayoutManager);

                stateFragmentBinding.stateRecyclerView.addItemDecoration(new DividerItemDecoration(stateFragmentBinding.stateRecyclerView.getContext(),
                        linearLayoutManager.getOrientation()));
                stateFragmentBinding.stateRecyclerView.setHasFixedSize(true);

                stateAdapter = new StateAdapter(lstStates, this);
                stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
            }
            else{
                stateAdapter.notifyDataSetChanged(lstStates);
            }
        }

    }
尝试以下操作:将数据更新方法名称更改为updateDataSet,并尝试一下

RecyclerView代码:

   class StateAdapter extends RecyclerView.Adapter<StateViewHolder> {
    private LayoutInflater inflater;
    private StateClickListener stateClickListener;
    private List<State> lstStateItem;

    public StateAdapter(List<State> lstStateItem, StateClickListener stateClickListener) {
        this.lstStateItem = lstStateItem;
        this.stateClickListener = stateClickListener;
    }

    @NonNull
    @Override
    public StateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        if (inflater == null)
            inflater = LayoutInflater.from(viewGroup.getContext());
        StateAdapterBinding stateAdapterBinding = StateAdapterBinding.inflate(inflater, viewGroup, false);
        return new StateViewHolder(stateAdapterBinding, stateClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int position) {
        State state = lstStateItem.get(position);
        stateViewHolder.bind(state, position);
    }

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

    public void notifyDataSetChanged(List<State> states) {
        List<State> tempList = new ArrayList<>(states);
        lstStateItem.clear();
        lstStateItem.addAll(tempList);
        this.notifyDataSetChanged();
    }
}
private void showStateList(List<State> states) {
        if (states != null) {
            this.lstStates = states;
            if (stateAdapter == null) {
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                stateFragmentBinding.stateRecyclerView.setLayoutManager(linearLayoutManager);

                stateFragmentBinding.stateRecyclerView.addItemDecoration(new DividerItemDecoration(stateFragmentBinding.stateRecyclerView.getContext(),
                        linearLayoutManager.getOrientation()));
                stateFragmentBinding.stateRecyclerView.setHasFixedSize(true);

                stateAdapter = new StateAdapter(lstStates, this);
                stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
            } else {
                stateAdapter.updateDataSet(lstStates);
            }
        }
    }
class StateAdapter extends RecyclerView.Adapter<StateViewHolder> {
    private LayoutInflater inflater;
    private StateClickListener stateClickListener;
    private List<State> lstStateItem = new ArrayList<>();;

    public StateAdapter(List<State> lstStateItem, StateClickListener stateClickListener) {
        this.lstStateItem.addAll(lstStateItem);
        this.stateClickListener = stateClickListener;
    }

    @NonNull
    @Override
    public StateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        if (inflater == null)
            inflater = LayoutInflater.from(viewGroup.getContext());
        StateAdapterBinding stateAdapterBinding = StateAdapterBinding.inflate(inflater, viewGroup, false);
        return new StateViewHolder(stateAdapterBinding, stateClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int position) {
        State state = lstStateItem.get(position);
        stateViewHolder.bind(state, position);
    }

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

    public void updateDataSet(List<State> states) {
        this.lstStateItem.clear();
        this.lstStateItem.addAll(states);
        notifyDataSetChanged();
    }
}
class StateViewHolder extends RecyclerView.ViewHolder {
    private StateAdapterBinding stateAdapterBinding;
    private StateClickListener stateClickListener;

    StateViewHolder(StateAdapterBinding stateAdapterBinding, StateClickListener stateClickListener) {
        super(stateAdapterBinding.getRoot());
        this.stateAdapterBinding = stateAdapterBinding;
        this.stateClickListener = stateClickListener;
    }

    public void bind(State state, int position) {
        stateAdapterBinding.setStateModel(state);
        stateAdapterBinding.setStateHandler(stateClickListener);
        stateAdapterBinding.getRoot().setTag(R.id.lnrHome, position);
        stateAdapterBinding.getRoot().setTag(state);
        stateAdapterBinding.executePendingBindings();
    }
尝试使用:

 your_binding_instance.executePendingBindings()
您的viewholder类应该如下所示:

    class ViewHolder(binding) extends RecyclerView.ViewHolder(binding.root){
    bind(item){
    bindnig.item = item
    binding.executePendingBindings()
    }
}

我已通过以下代码更改了showStateList方法,如果有更好的解决方案,请发布:

   private void showStateList(List<State> states) {
    if (states != null) {
        this.lstStates = states;
        if(stateAdapter == null){
            stateAdapter = new StateAdapter(lstStates, this);
        }
        else {
            stateAdapter.notifyStateChanged(lstStates);
        }
        stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
    }

}
private void showStateList(列表状态){
如果(状态!=null){
this.lstStates=状态;
if(stateAdapter==null){
stateAdapter=新的stateAdapter(lstStates,this);
}
否则{
stateAdapter.notifyStateChanged(lstStates);
}
stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
}
}

发布适配器的完整代码。问题不完整。。发布适配器代码…发布完整适配器代码@Kannan_SJDPosted完整适配器代码@DeepPatel@Praneshsaw:检查我的答案,然后尝试类似的问题!!请现在检查最后一次尝试。其他一切看起来都很好。如果仍然无法工作,则必须进行逐步调试,以检查数据丢失的位置。因为您使用了数据绑定。。我认为在列表更新后强制recyclerview执行挂起的绑定非常重要!我已经给出了stateAdapterBinding.executePendingBindings();在onCreateViewHolder中,但无法修复该问题。您将其称为何处?适配器内部;我想我必须在我的视图中调用它,即FragmentNo..我试图更改showStateList()方法,但它开始工作了,我不知道如何工作…只是想弄明白…每次都需要设置适配器…任何其他解决方案都将不胜感激。
   private void showStateList(List<State> states) {
    if (states != null) {
        this.lstStates = states;
        if(stateAdapter == null){
            stateAdapter = new StateAdapter(lstStates, this);
        }
        else {
            stateAdapter.notifyStateChanged(lstStates);
        }
        stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
    }

}