gpt4 book ai didi

android - 从另一个适配器刷新 recyclerview 适配器的值

转载 作者:太空狗 更新时间:2023-10-29 14:46:13 25 4
gpt4 key购买 nike

我必须在一个 Activity 中使用不同适配器的两个数据集,我想使用 notifyDataSetChanged() 但是它不适用于另一个适配器。所以在我的 adapterFirst 类中,我有 LongCLickListener 正在使用 notifyDataSetChanged() 我想调用 adapterSecond 来如果更新了 adapterFirst,也会通知。按下 LongClick 时 AdapterSecond 数据未更新

 public class AdapterFirst extends RecyclerView.Adapter <AdapterFirst .ViewHolder> {

private List<Object> objectList;
private Context mContext;
AdapterSecond adapterSecond;

public AdapteFirst(List<Object> list) { objectList= list; }

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
//query method
notifyDataSetChanged(); //working

//here it gets the nullpointerexception error
adapterSecond = new AdapteSecond();
adapterSecond.notifyDataSetChanged();
}
});
}

@Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data;

public ViewHolder(View itemView) {
super(itemView);
data= (TextView) itemView.findViewById(R.id.text);
}
}
}

第二个适配器

    public class AdapterSecond extends 
RecyclerView.Adapter <AdapterSecond.ViewHolder> {

private List<Object> objectList;
private Context mContext;

public AdapterSecond () {}

public AdapterSecond (List<Object> list) { objectList= list; }

@Override
public AdapterSecond.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_2, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final AdapterSecond.ViewHolder holder, int position) {

final Object object = objectList.get(holder.getAdapterPosition());
holder.data2.setText(object.getData());

}
@Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data2;

public ViewHolder(View itemView) {
super(itemView);
data2= (TextView) itemView.findViewById(R.id.text2);
}
}
}

最佳答案

似乎适配器的初始化顺序有一些问题。

只需确保在第一个适配器之前初始化第二个适配器

secondAdapter= new SecondAdapter();
firstAdapter = new FirstAdapter();

只需检查它们初始化的顺序!

关于android - 从另一个适配器刷新 recyclerview 适配器的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com