问题
I have objects in my firebase database called userForm . Each userForm have an instance variable called isPassedInspection that is either set to true or false in my firebase.
users can send Form object to my firebase, therefore I manually set isPassedInspection to true once I consider the form they send as "approved" by my standards.
I would like that my RecyclerView only create CardViews for userForm that it's isPassedInspection is true, otherwise, don't create a CardView (return null).
This is my code:
adapter = new FirebaseRecyclerAdapter<userForm, userFormViewHolder>(options) {
boolean isFormInspected;
@Override
public userFormViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
isPassedFormQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
Log.e(TAG,"datasnapshot doesn't exist in db");
}
for(DataSnapshot singleSnapshot: dataSnapshot.getChildren()){
if(singleSnapshot.exists()){
//get the boolean variable from firebase for this item, and set it to "isFormInspected"
isFormInspected = singleSnapshot.getValue(userForm.class).isPassedInspection();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// if form is manually inspected create a CardView for that form, else return null
if(isFormInspected){
CardView cv =(CardView)LayoutInflater.from(parent.getContext())
.inflate(R.layout.form_card, parent, false);
Log.e(TAG, "Created a CardView");
return new userFormViewHolder(cv);
}
else{
Log.e(TAG,"Form is not inspected,so dont create a CardView");
return null;
}
}
even though I know for sure that my item isPassedInspection is true, I always get this log I made:
Form is not inspected,so dont create a CardView
and after that this error:
java.lang.NullPointerException: Attempt to write to field 'int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null object reference
any suggestions? thank you!
回答1:
Use your own adapter class. you can see the codes. UserForm.class
public class UserForm {
String name,birthday,hobby;
boolean isPassedInspection;
public UserForm(String name, String birthday, String hobby, boolean isPassedInspection) {
this.name = name;
this.birthday = birthday;
this.hobby = hobby;
this.isPassedInspection = isPassedInspection;
}
public UserForm() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public boolean isPassedInspection() {
return isPassedInspection;
}
public void setPassedInspection(boolean passedInspection) {
isPassedInspection = passedInspection;
}}
// adapter class
public class FormsAdapter extends RecyclerView.Adapter<FormsViewHolder> {
private ArrayMap<String,UserForm> formsList=new ArrayMap<>();
/*use arrayMap to get from data and key */
public FormsAdapter() {
formsList=new ArrayMap<>();
}
@Override
public FormsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// inflating card view item
View v = inflater.inflate(R.layout.form_card, parent, false);
return new FormsViewHolder(v);
}
@Override
public void onBindViewHolder(FormsViewHolder holder, int position) {
String itemKey=formsList.keyAt(position);
UserForm userForm=formsList.get(itemKey);
// set one forms data
holder.setFormsData(userForm);
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// handle click event
}
});
}
@Override
public int getItemCount() {
return formsList.size();
}
public void addAFormItem(String key,UserForm userForm)
{
if (!formsList.containsKey(key))
{
formsList.put(key,userForm);
notifyItemInserted(formsList.size());
}
} }
ViewHolder class
public class FormsViewHolder extends RecyclerView.ViewHolder {
public View view;
public FormsViewHolder(View itemView) {
super(itemView);
view=itemView;
}
public void setFormsData(UserForm userForm)
{
// initialise card views items and set value in them
TextView userName=(TextView)view.findViewById(R.id.userName);
userName.setText(userForm.getName());
}}
your fragment
public class FormsListFragment extends Fragment {
private DatabaseReference formsRef;
private FormsAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_forms_list, container, false);
/**/
RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter=new FormsAdapter();
recyclerView.setAdapter(adapter);
formsRef= FirebaseDatabase.getInstance().getReference().child("forms");
formsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
/*add data in adapter one by one if isPassedInspection true*/
String itemKey=dataSnapshot.getKey();
UserForm userForm=dataSnapshot.getValue(UserForm.class);
if (userForm.isPassedInspection())
adapter.addAFormItem(itemKey,userForm);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
/*when something change in data then add in adapter if isPassedInspection true*/
String itemKey=dataSnapshot.getKey();
UserForm userForm=dataSnapshot.getValue(UserForm.class);
if (userForm.isPassedInspection())
adapter.addAFormItem(itemKey,userForm);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}}
来源:https://stackoverflow.com/questions/48633754/create-a-cardview-only-if-object-have-a-specific-value-in-firebase