Long winded question. I have already this week set up a recycler view for my users in the firebase database. I want to do the same for user incidents. These are concussion tests. I basically copied my player list activities and tried to implement the same for the incidents. However they are stored differently in firebase . You can see this from the picture below. I basically want to know is there a easy way to do this Ive tried a few things but no luck yet. At the minute here is my code I am using. The error I get with this is "com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String" although I think there is a few more errors.
Incident Helper Class
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;
}
}
Incident Adapter
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);
}
}
}
Incident Activity
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();
}
});
}
}
Update
@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());




