[英]Inflate a child view in a recyclerview when getting data from Firebase with Android
I have the following Activity which get called when a user clicks on a FAB我有以下活动,当用户单击 FAB 时会调用它
public class PicksheetActivity extends AppCompatActivity {
RecyclerView recyclerView;
GameAdapter adapter;
List<Games> gameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picksheet_layout);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
gameReference = mFirebaseDatabase.getReference("Games");
gameQuery = gameReference.equalTo(1);
// Add handle for listener
mListener = gameQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot gameSnapshot : dataSnapshot.getChildren()) {
Games game = new Games(gameSnapshot);
//
gameList.add(game);
adapter = new GameAdapter(this, gameList); <--- Error here
recyclerView.setAdapter(adapter);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("SurvivorPickSheetActivity", "onCancelled", databaseError.toException());
}
}
});
}
The GameAdapter class is as follows: GameAdapter class 如下:
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {
public GameAdapter(Context mCtx, List<Games> gameList) {
this.mCtx = mCtx;
this.gameList = gameList;
Log.d("CREATION", "*** CALLING THE GameAdapter class ***");
}
@Override
public GameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.list_layout, parent,false);
GameViewHolder gameViewHolder = new GameViewHolder(view);
return gameViewHolder;
}
}
The compiler is telling me to change the GameAdapter's class constructor from Context
to ValueEventListener
since I guess I'm making the call to Firebase编译器告诉我将 GameAdapter 的 class 构造函数从
Context
更改为ValueEventListener
因为我想我正在调用 Firebase
If I do that then the GameAdapter onCreateViewHolder
wont work since the LayoutInflater.from() function requires Context and not a ValueEventListener to inflate the view?!如果我这样做,那么 GameAdapter
onCreateViewHolder
将无法工作,因为 LayoutInflater.from() function 需要 Context 而不是 ValueEventListener 来膨胀视图?!
Can anyone help?任何人都可以帮忙吗?
adapter = new GameAdapter(this, gameList);
适配器=新游戏适配器(这个,游戏列表); <--- Error here
<--- 这里有错误
Here this
refers to the surrounding class which is the anonymous inner class created by new ValueEventListener() {
instance and it is not a context
. this
指的是周围的 class ,它是由new ValueEventListener() {
instance 创建的匿名内部 class ,它不是context
。
In order to refer to the outer class to get the context, you can replace this
with PicksheetActivity.this
为了参考外部 class 来获取上下文,您可以将
this
替换为PicksheetActivity.this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.