当前位置: 首页 > 知识库问答 >
问题:

Firebase实时数据库向回收器添加值

蒙勇
2023-03-14
public class IncidentHelperClass {

    //Declare variables of the information to store in the database
    String playerId, Cervical_Spine_Activity, Glasgow_Coma_Scale_Test, Memory_Questions, Observable_Signs_Test, Red_Flags_Test, Report;

    //Empty constructor
    public IncidentHelperClass() {

    }

    //Constructor
    public IncidentHelperClass(String redFlag, String observable, String memory, String gcs, String csa, String incidentReport) {

        this.Cervical_Spine_Activity = redFlag;
        this.Observable_Signs_Test = observable;
        this.Memory_Questions = memory;
        this.Glasgow_Coma_Scale_Test = gcs;
        this.Cervical_Spine_Activity = csa;
        this.Report = incidentReport;
    }

    public String getPlayerId() {
        return playerId;
    }

    public void setPlayerId(String playerId) {
        this.playerId = playerId;
    }

    public String getCervical_Spine_Activity() {
        return Cervical_Spine_Activity;
    }

    public void setCervical_Spine_Activity(String cervical_Spine_Activity) {
        Cervical_Spine_Activity = cervical_Spine_Activity;
    }

    public String getGlasgow_Coma_Scale_Test() {
        return Glasgow_Coma_Scale_Test;
    }

    public void setGlasgow_Coma_Scale_Test(String glasgow_Coma_Scale_Test) {
        Glasgow_Coma_Scale_Test = glasgow_Coma_Scale_Test;
    }

    public String getMemory_Questions() {
        return Memory_Questions;
    }

    public void setMemory_Questions(String memory_Questions) {
        Memory_Questions = memory_Questions;
    }

    public String getObservable_Signs_Test() {
        return Observable_Signs_Test;
    }

    public void setObservable_Signs_Test(String observable_Signs_Test) {
        Observable_Signs_Test = observable_Signs_Test;
    }

    public String getRed_Flags_Test() {
        return Red_Flags_Test;
    }

    public void setRed_Flags_Test(String red_Flags_Test) {
        Red_Flags_Test = red_Flags_Test;
    }

    public String getReport() {
        return Report;
    }

    public void setReport(String report) {
        Report = report;
    }

事件适配器

public class IncidentListAdapter extends RecyclerView.Adapter<IncidentListAdapter.ViewHolder> {


    //Constants for the player name and id
    public static final String PLAYER_NAME = "playername";
    public static final String PLAYER_ID = "playerid";


    SharedPreferences sharedpreferences;

    public static final String MyPREFERENCES = "MyPrefs";
    public static final String ID = "idKey";
    //Variables
    private Context context;
    //Declare a list using the UserHelperClass
    private List<IncidentHelperClass> incidentList;

    //Constructor
    public IncidentListAdapter(Context context, List<IncidentHelperClass> incidentsList) {
        this.context = context;
        this.incidentList = incidentsList;
    }

    @NonNull
    @Override
    //If viewHolder does  not exist create one by inflating the user_details_view
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(
                LayoutInflater.from(context)
                        .inflate(R.layout.users_details_view, parent, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {


        //Retrieve the incident stored at the position
        final IncidentHelperClass incidents = incidentList.get(position);
        //Set the info from the UserHelperClass in the textViews
        holder.textViewRedFlag.setText(incidents.getRed_Flags_Test());
        holder.textViewObs.setText(incidents.getObservable_Signs_Test());
        holder.textViewMemory.setText(incidents.getMemory_Questions());
        holder.textViewGCS.setText(incidents.getGlasgow_Coma_Scale_Test());
        holder.textViewCSA.setText(incidents.getCervical_Spine_Activity());
        holder.textViewIncidentReport.setText(incidents.getReport());


        //Add an onClickListener to the parentView
        holder.parentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                IncidentHelperClass selectedIncident = incidentList.get(position);

                //The intent to launch the activity
                Intent intent = new Intent(context, RedFlagActivity.class);

                //Put the player id and name into the Intent from the playerList
                //  intent.putExtra(PLAYER_ID, player.getPlayerID());
                // intent.putExtra(PLAYER_NAME, player.getName());
                //Start the activity passed from the intent
                context.startActivity(intent);

            }
        });


    }

    @Override
    //Return the amount of players
    public int getItemCount() {
        return this.incidentList.size();
    }


    //ViewHolder wraps the view passed to it so RecyclerView can deal with it
    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textViewRedFlag;
        private TextView textViewObs;
        private TextView textViewMemory;
        private TextView textViewGCS;
        private TextView textViewCSA;
        private TextView textViewIncidentReport;
        private View parentView;


        //Attach the variables to their views
        public ViewHolder(@NonNull View view) {
            super(view);
            this.parentView = view;
            this.textViewRedFlag = view.findViewById(R.id.player_view_name);
            this.textViewObs = view.findViewById(R.id.player_view_email);
            this.textViewMemory = view.findViewById(R.id.player_view_phoneNo);
            this.textViewGCS = view.findViewById(R.id.player_view_emergencyContact);
            this.textViewCSA = view.findViewById(R.id.player_view_emergencyContactPhone);
            this.textViewGCS = view.findViewById(R.id.player_view_emergencyContactPhone2);
            this.textViewIncidentReport = view.findViewById(R.id.player_view_emergencyContactPhone3);
        }
    }
}

事件活动

public class IncidentListView extends AppCompatActivity {


//Class to update and display information in the recycler_view


    //Define the RecyclerView
    RecyclerView listViewIncidents;

    //Define a list to store the players
    ArrayList<IncidentHelperClass> incidentsList;
    //Define the reference to the database
    DatabaseReference reference;

    //Define the PlayerList Adapter
    IncidentListAdapter adapter;


    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs";
    public static final String ID = "idKey";

    //Constants fot the player name and id
    public static final String PLAYER_NAME = "playername";
    public static final String PLAYER_ID = "playerid";

    //Tag for printing log details
    private final String TAG = getClass().getSimpleName();

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

        //Assign the listView to the appropriate view
        // listViewPlayers = findViewById(R.id.users_list_recycler_view);
        //Assign the playersList
        incidentsList = new ArrayList<>();

        //Assign the recycler view to the correct view
        listViewIncidents = findViewById(R.id.recycler_list_view);
        //Set the adapter to an instance of the PlayerListAdapter
        listViewIncidents.setAdapter(new IncidentListAdapter(this, incidentsList));
        //Instruct the layout manager to set the layout to LinearLayout
        listViewIncidents.setLayoutManager(new LinearLayoutManager(this));

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        sharedpreferences = getBaseContext().getSharedPreferences(MyPREFERENCES, 0);
        String id = sharedpreferences.getString(ID, "UserID");
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(ID, id);
        editor.commit();

        //Get a reference to the path required in the database
        reference = FirebaseDatabase.getInstance().getReference("Incidents");

        //AddValueEventListener will update the players list if any new players added
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot incidentSnapshot : snapshot.getChildren()) {
                    //Get the values defined in the UserHelperClass from the registered players in the database
                    IncidentHelperClass incident = incidentSnapshot.getValue(IncidentHelperClass.class);
                    //If a new player is created add them to the playersList
                    incidentsList.add(incident);
                }
                //What does this do??
                adapter = new IncidentListAdapter(IncidentListView.this, incidentsList);
                listViewIncidents.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                //Show Error toast to user
                Toast.makeText(IncidentListView.this, "Error Occured", Toast.LENGTH_LONG).show();
            }
        });
    }
}

@override public void onBindViewHolder(@nonnull ViewHolder holder,final int position){

//Retrieve the incident stored at the position
final StackIncidentHelperClass incidents = incidentList.get(position);
//Set the info from the UserHelperClass in the textViews
holder.textViewRedFlag.setText((CharSequence) incidents.getRed_Flags_Test());
holder.textViewObs.setText((CharSequence) incidents.getObservable_Signs_Test());
holder.textViewMemory.setText((CharSequence) incidents.getMemory_Questions());
holder.textViewGCS.setText((CharSequence) incidents.getGlasgow_Coma_Scale_Test());
holder.textViewCSA.setText((CharSequence) incidents.getCervical_Spine_Activity());
holder.textViewIncidentReport.setText((CharSequence) incidents.getReport());

共有1个答案

司寇山
2023-03-14
String Cervical_Spine_Activity, Glasgow_Coma_Scale_Test, Memory_Questions, Observable_Signs_Test, Red_Flags_Test;

它们的类型是java.util.HashMap

class Report {
    public String reportDetails;
    public String reportId;
    // add getters/setters/constructors if necessary
}

有了这些信息,您就可以更新IncidentHelperClass类成员声明:

public class IncidentHelperClass {

    //Declare variables of the information to store in the database
    String playerId;
    HashMap<String, String> Cervical_Spine_Activity, Glasgow_Coma_Scale_Test, Memory_Questions, Observable_Signs_Test, Red_Flags_Test;
    @SerializedName("Report")
    Report report;

    //Empty constructor
    public IncidentHelperClass() {

    }

    //Constructor
    public IncidentHelperClass(HashMap<String, String> redFlag, 
                               HashMap<String, String> observable, 
                               HashMap<String, String> memory, 
                               HashMap<String, String> gcs, 
                               HashMap<String, String> csa, 
                               Report incidentReport) {

        this.Cervical_Spine_Activity = redFlag;
        this.Observable_Signs_Test = observable;
        this.Memory_Questions = memory;
        this.Glasgow_Coma_Scale_Test = gcs;
        this.Cervical_Spine_Activity = csa;
        this.report = incidentReport;
    }

    public String getPlayerId() {
        return playerId;
    }

    public void setPlayerId(String playerId) {
        this.playerId = playerId;
    }

    public HashMap<String, String> getCervical_Spine_Activity() {
        return Cervical_Spine_Activity;
    }

    public void setCervical_Spine_Activity(HashMap<String, String> cervical_Spine_Activity) {
        Cervical_Spine_Activity = cervical_Spine_Activity;
    }

    public HashMap<String, String> getGlasgow_Coma_Scale_Test() {
        return Glasgow_Coma_Scale_Test;
    }

    public void setGlasgow_Coma_Scale_Test(HashMap<String, String> glasgow_Coma_Scale_Test) {
        Glasgow_Coma_Scale_Test = glasgow_Coma_Scale_Test;
    }

    public HashMap<String, String> getMemory_Questions() {
        return Memory_Questions;
    }

    public void setMemory_Questions(HashMap<String, String> memory_Questions) {
        Memory_Questions = memory_Questions;
    }

    public HashMap<String, String> getObservable_Signs_Test() {
        return Observable_Signs_Test;
    }

    public void setObservable_Signs_Test(HashMap<String, String> observable_Signs_Test) {
        Observable_Signs_Test = observable_Signs_Test;
    }

    public HashMap<String, String> getRed_Flags_Test() {
        return Red_Flags_Test;
    }

    public void setRed_Flags_Test(HashMap<String, String> red_Flags_Test) {
        Red_Flags_Test = red_Flags_Test;
    }

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        this.report = report;
    }
}
 类似资料:
  • 我无法在Firebase实时数据库中添加任何数据。数据库连接正常,但我无法添加任何数据。当我单击“到”按钮时,添加到数据库中的数据。 主要活动。java-代码 事件。java-代码

  • 我在文档中看到Firebase实时数据库是加密的<但是有一些术语我不完全理解: Firebase服务使用HTTPS加密传输中的数据,并从逻辑上隔离客户数据。什么在逻辑上隔离了客户数据 平均值 “此外,几个Firebase服务还加密其静态数据:Firebase实时数据库Firebase测试实验室”有人能用外行的术语解释一下吗

  • 我在一个Android应用程序中使用Firebase数据库,每次用户启动时,我都会在数据库中存储一些值,为此我会执行以下操作: 正如您在子方法中看到的,如果称为“usrId”,它将创建usrId目录,并在其中添加所有neccesary信息。但是我想为每个用户创建目录,所以我尝试传递usrId变量作为参数。但它不起作用。当我调试代码时,调试器说本地var usrId无法识别,我的问题是如何在Fire

  • 我有一个,它将活动时间数据注册到我的Firebase实时数据库中,这是使用手动输入的,然后将其显示到UI中的。这一切都很好,但是,如果我要输入更多数据,它只需替换数据库中的值和。 我以前在另一个活动中做过类似的事情(将多个Firebase子值相加(合计)以得到总价值?),但是,这个活动有额外的带日期的节点等,所以同样的方法在我当前的活动中不起作用。理想情况下,我将使用dateStrings等组织此

  • 我想补充一点 我正在添加新的消息,下面的代码是我从Firebase文档中学到的。 我的模型是: } 和firebase侦听器 RecyclerView适配器: 很抱歉读了这么长的帖子,谢谢你抽出时间。