public class Result extends AppCompatActivity {
DrawerLayout drawerLayout;
private static final String TAG = "Result";
private EditText mNodeNameEditText;
private DatabaseReference mDatabaseRef;
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
private RecyclerView mRecyclerView;
private RecyclerViewAdapter mAdapter;
private ArrayList<UserItem> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
drawerLayout = findViewById(R.id.drawer_activity);
mNodeNameEditText = findViewById(R.id.node_name_edit_text);
// Set up RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mData = new ArrayList<>();
mAdapter = new RecyclerViewAdapter(Result.this, mData);
mRecyclerView.setAdapter(mAdapter);
// Set default node of the database
mDatabaseRef = FirebaseDatabase.getInstance().getReference("predict");
mNodeNameEditText.removeTextChangedListener(textWatcher);
mNodeNameEditText.addTextChangedListener(textWatcher);
// Load data based on the current EditText input
handleEditTextInput();
}
// TextWatcher implementation
private final TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
handleEditTextInput();
}
};
// Create a new method to handle EditText input
private void handleEditTextInput() {
String nodeName = mNodeNameEditText.getText().toString().trim();
if (!nodeName.isEmpty()) {
retrieveData(nodeName);
} else {
Toast.makeText(Result.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
}
}
private void retrieveData(String phoneNumber) {
DatabaseReference myRef = mDatabaseRef;
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.d(TAG, "Data snapshot exists: " + dataSnapshot.toString());
} else {
Log.d(TAG, "Data snapshot doesn't exist");
}
mData.clear();
for (DataSnapshot idSnapshot : dataSnapshot.getChildren()) {
UserData userData = idSnapshot.getValue(UserData.class);
if (userData != null) {
Log.d(TAG, "Data received: " + userData.toString());
mData.add(new UserItem("Node Name", idSnapshot.getKey())); // Add this line
mData.add(new UserItem("Soreness Label", userData.soreness_label));
mData.add(new UserItem("EMG Data", userData.emg_data));
mData.add(new UserItem("GSR Data", userData.gsr_data));
mData.add(new UserItem("Timestamp", userData.timestamp));
} else {
Log.d(TAG, "UserData object is null");
}
}
mAdapter.notifyDataSetChanged(); // Add this line
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "Failed to read value.", databaseError.toException());
}
});
}
public void ClickMenu(View view) {
MainActivity.openDrawer(drawerLayout);
}
public void ClickLogo(View view) {
MainActivity.closeDrawer(drawerLayout);
}
public void ClickHome(View view) {
MainActivity.redirectActivity(this, MainActivity.class);
}
public void ClickProfile(View view) {
MainActivity.redirectActivity(this, Profile.class);
}
public void ClickResult(View view) {
recreate();
}
public void ClickAction(View view) {
MainActivity.redirectActivity(this, Action.class);
}
public void ClickLogout(View view) {
MainActivity.logout(this);
}
@Override
protected void onPause() {
super.onPause();
MainActivity.closeDrawer(drawerLayout);
}
}
UserItem.java
package com.example.profile;
public class UserItem {
private String name;
private String data;
code
public UserItem(String name, Object data) {
this.name = name;
this.data = data.toString(); // Just convert the object directly to a string
}
public String getName() {
return name;
}
public String getData() {
return data;
}
}
UserData.java
package com.example.profile;
public class UserData {
code
public String soreness_label;
public int emg_data; // Changed to Object
public int gsr_data; // Changed to Object
public String timestamp;
public UserData() {
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
code
private ArrayList<UserItem> mData;
private LayoutInflater mInflater;
// data is passed into the constructor
public RecyclerViewAdapter(Context context, ArrayList<UserItem> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the cell layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextViews in each cell
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
UserItem userItem = mData.get(position);
holder.nameTextView.setText(userItem.getName());
holder.dataTextView.setText(userItem.getData());
}
// total number of cells
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
TextView dataTextView;
ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.textview_name);
dataTextView = itemView.findViewById(R.id.textview_data);
}
}
To remove the EditText from your Result activity, you'll need to make changes in both the Java code (Result.java) and the layout XML file (activity_result.xml). Here's a step-by-step guide on how to do it:
1. Modify activity_result.xml:
Remove the EditText block from the activity_result.xml file. The block to remove is:
After removing the EditText, you might want to adjust the constraints of the RecyclerView to ensure it positions correctly. Update the app:layout_constraintTop_toBottomOf attribute of the RecyclerView to point to the new element above it (in this case, the include element with the id @+id/include is already the top element, so you might not need to change anything, but if you had other elements, you'd adjust it accordingly). The updated RecyclerView block should look something like this (though, as mentioned, the change might not be necessary in your specific layout):
xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include" <!-- Ensure this points to the correct element -->
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp" />
2. Modify Result.java:
Remove the EditText variable and its initialization:
Remove private EditText mNodeNameEditText;
Remove mNodeNameEditText = findViewById(R.id.node_name_edit_text); from onCreate(Bundle savedInstanceState)
Remove TextWatcher and its related code:
Remove the entire textWatcher block.
Remove mNodeNameEditText.removeTextChangedListener(textWatcher); and mNodeNameEditText.addTextChangedListener(textWatcher); from onCreate(Bundle savedInstanceState).
Modify or Remove handleEditTextInput():
Since this method relies on the EditText for input, you'll either need to modify it to handle data retrieval without user input (e.g., retrieve all data, use a default node, etc.) or remove it if not necessary.
If you choose to modify it for a default behavior, you might directly call retrieveData with a predefined node name. Example:
java
private void handleDefaultDataRetrieval() {
String defaultNodeName = "YourDefaultNode"; // Define your default node name
retrieveData(defaultNodeName);
}
code
Then, call `handleDefaultDataRetrieval()` in `onCreate` instead of `handleEditTextInput()`.
4. **Remove the Call to `handleEditTextInput()`**:
- Remove `handleEditTextInput();` from `onCreate(Bundle savedInstanceState)`.
5. **Adjust `retrieveData` Call (If Necessary)**:
- If you've removed all references to user-input-driven data retrieval, ensure that `retrieveData` is called with appropriate parameters if you still want to load data when the activity starts. You might call it with a default node name as shown in the `handleDefaultDataRetrieval` example above.
**Example of Modified `onCreate` in `Result.java` (assuming you've chosen to use a default node for data retrieval):**
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
drawerLayout = findViewById(R.id.drawer_activity);
// Set up RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mData = new ArrayList<>();
mAdapter = new RecyclerViewAdapter(Result.this, mData);
mRecyclerView.setAdapter(mAdapter);
// Set default node of the database
mDatabaseRef = FirebaseDatabase.getInstance().getReference("predict");
// Retrieve data using a default node
handleDefaultDataRetrieval();
// ... (Rest of your onCreate code)
}
Note: This guide assumes you want to remove the EditText entirely. If you're looking to temporarily hide it or disable user input, you would use setVisibility or setEnabled methods on the EditText object instead.