回收站适配器未显示数据

时间:2019-06-14 22:54:52

标签: java android firebase-realtime-database recycler-adapter

我正在使用Firebase创建一个应用程序,用户可以在其中注册一个课程,然后查看它们。我将相同的回收站适配器和对象用于显示类和显示用户类在其uid下的用户的活动。当我运行该应用程序时,这些类会显示我已经输入到Firebase中,但是显示用户类的活动会显示一个空白的cardview(使用相同的代码)。错误是:`

No setter/field for room_number2 found on class com.samuelford48gmail.thsconnect.Class_model
No setter/field for date_clasname2 found on class com.samuelford48gmail.thsconnect.Class_model
No setter/field for teacher2 found on class com.samuelford48gmail.thsconnect.Class_model

我显示用户类的片段的代码:

public class home_fragment extends Fragment implements View.OnClickListener {
private Button button;
//DatabaseReference dref;
//ListView listview2;
//ArrayList<String> list=new ArrayList<>();
private FirebaseDatabase database;
private DatabaseReference myRef;
private List<Listdata> list;
private RecyclerView recyclerview;
public home_fragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_fragment, container, false);
    //FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();
    //if(fbUser == null) { Intent intent = new Intent(getContext(), LoginActivity.class);
   // startActivity(intent);}

    button = (Button) view.findViewById(R.id.button);
    button.setOnClickListener(this);
    recyclerview = (RecyclerView) view.findViewById(R.id.rview);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Users")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Classes");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list = new ArrayList<>();
            // StringBuffer stringbuffer = new StringBuffer();
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                Class_model new_class = dataSnapshot1.getValue(Class_model.class);
                String nameofclass = new_class.getDate_clasname();
                String teacherofclass = new_class.getTeacher();
                String roomnumberofclass = new_class.getRoom_number();
                String class_key = new_class.getUid();
                Listdata listdata = new Listdata(nameofclass, teacherofclass, roomnumberofclass, class_key);
                //String name = userdetails.getName();
                //String email = userdetails.getEmail();
                //String address = userdetails.getAddress();
                listdata.setDate_class(nameofclass);
                listdata.setTeacher(teacherofclass);
                listdata.setRnumber(roomnumberofclass);
                list.add(listdata);
                // Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();

            }

            RecyclerviewAdapter2 recycler = new RecyclerviewAdapter2(list);
            RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getContext());
            recyclerview.setLayoutManager(layoutmanager);
            recyclerview.setItemAnimator(new DefaultItemAnimator());
            recyclerview.setAdapter(recycler);

        }

        @Override
        public void onCancelled(DatabaseError error) {
            AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Check your connection! If, problem persists please email svhsdev@vigoschools.org!");
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            alertDialog.show();
            // Failed to read value
            //  Log.w(TAG, "Failed to read value.", 
error.toException());
        }
    });
 return view;
}
@Override
public void onClick(View view) {
    signout();



    //startActivity(new Intent(home_fragment.this, 
LoginActivity.class));

}
public void signout(){
    FirebaseAuth.getInstance().signOut();
    Intent intent = new Intent(getContext(), LoginActivity.class);
    startActivity(intent);
  }
 }

这是我的Recycler Adapter类的代码:

public class RecyclerviewAdapter2 extends RecyclerView.Adapter<RecyclerviewAdapter2.MyHolder>{

List<Listdata> listdata;

public RecyclerviewAdapter2(List<Listdata> listdata) {
    this.listdata = listdata;
}

@Override
public RecyclerviewAdapter2.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_class_model,parent,false);

    RecyclerviewAdapter2.MyHolder myHolder = new RecyclerviewAdapter2.MyHolder(view);
    return myHolder;
}


public void onBindViewHolder(RecyclerviewAdapter2.MyHolder holder, final int position) {
    final Listdata data = listdata.get(position);
    holder.vdate_class.setText(data.getDate_class());
    holder.vteacher.setText(data.getTeacher());
    holder.vrnumber.setText(data.getRnumber());
    //System.out.println(data.getDate_class2());
    holder.itemView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick( final View view) {
            Context context = view.getContext();
            Intent intent = new Intent(context, test_for_science_add_class.class);
            intent.putExtra("date_class", listdata.get(position).getDate_class());
            intent.putExtra("teacher", listdata.get(position).getTeacher());
            intent.putExtra("room_number", listdata.get(position).getRnumber());
            intent.putExtra("post_key", listdata.get(position).getUid());
            context.startActivity(intent);
        }
    });

}

@Override
public int getItemCount() {
    return listdata.size();
}


class MyHolder extends RecyclerView.ViewHolder{
    TextView vdate_class , vteacher,vrnumber;

    public MyHolder(View itemView) {
        super(itemView);
        vdate_class = (TextView) itemView.findViewById(R.id.date_class_name);
        vteacher = (TextView) itemView.findViewById(R.id.teacher);
        vrnumber = (TextView) itemView.findViewById(R.id.room_number);

    }
 }

}

这是我的Class_model代码:

public class Class_model {
//String subject;
String date_clasname;
String  teacher;
String room_number;
String uid;
public Class_model(){};
public Class_model(String date_classname, String teacher, String room_number, String uid){

    this.date_clasname = date_classname;
    this.teacher = teacher;
    this.room_number = room_number;
    this.uid = uid;

}
public String getDate_clasname() {
    return date_clasname;
}

public void setDate_clasname(String date_clasname) {
    this.date_clasname = date_clasname;
}

public String getTeacher() {
    return teacher;
}

public void setTeacher(String teacher) {
    this.teacher = teacher;
}

public String getRoom_number() {
    return room_number;
}

public void setRoom_number(String room_number) {
    this.room_number = room_number;
}
public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
 }
}

这是我的Listdata类的代码:

public class Listdata {

String date_class;
 String teacher;
 String rnumber;
 String uid;



public Listdata(String date_class, String teacher, String rnumber, String uid) {
    this.date_class = date_class;
    this.teacher = teacher;
    this.rnumber = rnumber;
    this.uid = uid;
}

public String getDate_class() {
    return date_class;
}

public void setDate_class(String date_class) {
    this.date_class = date_class;
}

public String getTeacher() {
    return teacher;
}

public void setTeacher(String teacher) {
    this.teacher = teacher;
}

public String getRnumber() {
    return rnumber;
}

public void setRnumber(String rnumber) {
    this.rnumber = rnumber;
}
public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
 }
}

我的Firebase结构:

"Technology" : {
"-LgxmDqmlPZnCOEyjBww" : {
  "date_clasname" : "technology",
  "room_number" : "d",
  "teacher" : "f"
}
  },
  "Users" : {
   "NnJlSVeqGXhJ2Nab2bhjr0HIpku2" : {
  "Classes" : {
    "-LhM7iyYhmwvtDvtLE-Z" : {
      "date_clasname2" : "technology",
      "room_number2" : "d",
      "teacher2" : "f"
    },
    "-LhM7k0uI_eY31nQnnro" : {
      "date_clasname2" : "other",
      "room_number2" : "d",
      "teacher2" : "f"
    },
    "-LhM87eT7kv7KL1JHryM" : {
      "date_clasname2" : "March 5",
      "room_number2" : "101",
      "teacher2" : "pence"
    }
  },
  "email" : "samuelford48@gmail.com",
  "grade" : "12",
  "name" : "Samuel Ford"
},

这是我的代码,当用户单击“回收者”视图项之一时发生的活动:

public class test_for_science_add_class extends AppCompatActivity {
    private Button add_class;
    private FirebaseDatabase database;
    private DatabaseReference myRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_for_science_addd_class);
        final String date_class2 = getIntent().getStringExtra("date_class");
        final String teacher = getIntent().getStringExtra("teacher");
        final String room_number  = getIntent().getStringExtra("room_number");
        final String post_key = getIntent().getStringExtra("post_key");
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Classes");
        //String name = getIntent().getExtra("date_class");
        //String city = getIntent().getExtra("City");
        //System.out.println(value);
        TextView display_class_name = (TextView) findViewById(R.id.date_tv2);
        display_class_name.setText(date_class2);
        TextView display_teacher = (TextView) findViewById(R.id.teacher_tv2);
        display_teacher.setText(teacher);
        TextView display_room_number = (TextView) findViewById(R.id.rn_tv2);
        display_room_number.setText(room_number);
        add_class = (Button) findViewById(R.id.add_class_2);
        add_class.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Class_model_without_uid new_class_to_user_uid = new Class_model_without_uid(date_class2, teacher, room_number);
                myRef.push().setValue(new_class_to_user_uid);


                Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();

            }
        });
    }


}

1 个答案:

答案 0 :(得分:0)

在处理对回收站项目的点击的活动中,当我推送到Firebase时,我调用了错误的对象类。

错误代码:

NavigationView {
    Text("Hello world")
        .navigationBarTitle(Text("Title"))
        .navigationBarItems(trailing:
            PresentationButton(destination: TestView()) {
                Text("Next")
            }
        )
}

正确的代码:

 Class_model_without_uid new_class_to_user_uid = new Class_model_without_uid(date_class2, teacher, room_number);
            myRef.push().setValue(new_class_to_user_uid);