gpt4 book ai didi

android - 没有连接适配器;跳过布局 : RecyclerView & Adapter

转载 作者:行者123 更新时间:2023-11-29 22:50:03 25 4
gpt4 key购买 nike

我已经看到很多关于这个问题的答案,但对我的编码没有任何帮助。

我引用了基于此 youtube 的代码视频,现在我遇到了这个问题。不确定他的代码是如何工作的,而我的则不是。显然我也是这个话题的新手(RecyclerView)。

这是我的主要 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_product) ;

ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").child("Products");
recyclerView = findViewById(R.id.stallproductRecyclerView);

//newest
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
//
searchView = findViewById(R.id.searchProductStall);

}

@Override
protected void onRestart() {
super.onRestart();
if (ref!=null)
{
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren())
{

list.add(ds.getValue(Model.class));
}
ViewHolder viewHolder = new ViewHolder(list);
recyclerView.setAdapter(viewHolder);
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ViewProduct.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}

这是 ViewHolder 类。

ArrayList<Model> list;
public ViewHolder (ArrayList<Model> list)
{
this.list=list;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product,
viewGroup,false);


return new MyViewHolder(view);


}

最佳答案

oncreate() 中使用空的 data 设置适配器,在 fetching 数据之后你应该调用 adapter.notifyDataSetChanged() 如下所示。

ViewHolder viewHolder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_product) ;

ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").child("Products");
recyclerView = findViewById(R.id.stallproductRecyclerView);

//newest
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
//
searchView = findViewById(R.id.searchProductStall);

// here you have to set the adapter to your recycler view
list = new ArrayList<>();
viewHolder = new ViewHolder(list);
recyclerView.setAdapter(viewHolder);

}

@Override
protected void onRestart() {
super.onRestart();
if (ref!=null)
{
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
// clear your previous data
list.clear();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
list.add(ds.getValue(Model.class));
}
// here you have to notify you adapter that your data set is changed like below
viewHolder.notifyDataSetChanged();
}

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ViewProduct.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}

关于android - 没有连接适配器;跳过布局 : RecyclerView & Adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045279/

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