当我单击RecyclerView时,我想捕获数据库中房间的数据,但是遇到了问题。单击RecyclerView时如何捕获此数据。我在ArrayAdapter部分中收到此错误。
错误:java.lang.NullPointerException:尝试调用虚拟 方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'上的null 对象引用
public class ProductsNewAdapter extends RecyclerView.Adapter<ProductsNewAdapter.ProductViewHolder> {
Context mCtx;
private Spinner spinner;
List<String> Rooms = new ArrayList<String>();
public ProductsNewAdapter(Context mCtx, List<ProductNew> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.product_laylout_new,
parent, false);
ProductViewHolder productViewHolder = new ProductViewHolder(view);
return productViewHolder;
}
int selectedPosition=-1;
@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
if(selectedPosition==position)
holder.itemView.setBackgroundColor(Color.parseColor("#25E9F9"));
else
holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
selectedPosition=position;
notifyDataSetChanged();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
DatabaseReference refRooms = mDatabase.child("0").child("Rooms");
refRooms.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
spinner = (Spinner) v.findViewById(R.id.spinnerMain);
ViewGroup parent = null;
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.product_laylout_new,
parent, false);
String data = snapshot.getValue(String.class);
Rooms.add(data);
// addListenerOnSpinnerItemSelection();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
答案 0 :(得分:1)
尝试一下
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_laylout_new,
parent, false);