我在我的firebase数据库中有名为 userform 的对象。每个 userform 都有一个称为 ispassedinspection 的实例变量,该变量在我的firebase中设置为true或false。
用户可以将表单对象发送到我的firebase,因此,一旦我将其按照我的标准视为"批准"的形式,我将手动设置为true true。
我希望我的recyclerview仅为用户形式创建cardViews ispassedinspection 是正确的,否则,请勿创建cardView(return null(。
这是我的代码:
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;
}
}
即使我确定我的物品 ispassedinspection 是真的,我总是会得到我制作的日志:
未检查表单,所以不要创建卡片浏览
之后,此错误:
java.lang.nullpointerexception:尝试写入字段'int android.support.v7.widget.RecyClerview $ viewholder.mitemviewtype' null对象参考
有什么建议吗?谢谢!
使用自己的适配器类。您可以看到代码。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;
}}
//适配器类
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());
}
} }
查看持有人类
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());
}}
你的片段
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;
}}