gpt4 book ai didi

java - 无法从 RecyclerView 适配器类调用方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:14 25 4
gpt4 key购买 nike

我创建了一个recyclerView -

public class PlayerListRecyclerViewAdapter extends RecyclerView.Adapter<PlayerListRecyclerViewAdapter.ViewHolder>


playerAdapter = new PlayerListRecyclerViewAdapter(this,cursor, playerDataSet);
playerRecyclerView.setAdapter(playerAdapter);

一切正常,但我创建了以下方法:

public void swapCursor(Cursor newCursor) {
// Always close the previous mCursor first
if (pCursor != null) pCursor.close();
pCursor = newCursor;
if (newCursor != null) {
// Force the RecyclerView to refresh
this.notifyDataSetChanged();
}
}

但是当我尝试从链接到适配器的 Activity 调用它时,它找不到它:

 new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//Do nothing, we are not moving anything
//But maybe we can let the user arrange their list?
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
long id = (long) viewHolder.itemView.getTag();
removePlayer(id);
playerAdapter.swapCursor(getAllPlayers());

}
}).attachToRecyclerView(playerRecyclerView);
}

交换光标是红色的,在适配器中它显示从未使用过的消息。

我正在尝试遵循:

https://github.com/udacity/ud851-Exercises/blob/student/Lesson07-Waitlist/T07.05-Solution-AddGuests/app/src/main/java/com/example/android/waitlist/GuestListAdapter.java

但我不确定我错过了什么?其他一切都工作正常,我可以滑动,它会加载详细信息。一切都很好,只是由于某种原因调用此方法不起作用。

最佳答案

请检查您的播放器适配器是否获取空值。

因为swapCursor方法是在定义playeradapter对象后访问的。

if(playerAdapter!=null){
playerAdapter.swapCursor(getAllPlayers());
}

关于java - 无法从 RecyclerView 适配器类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50100060/

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