1

So when I click my EditText field called Search, I get the keyboard, and for a brief second I see the vatical line you see when you are typing. But then the recyclerview is updated, and the typo input is removed, and the keyboard is still showing, but when I type nothing happens, until I click on the EditText field a second time. How can this be??

EditText

This is my Fragment where I have the Recyclerview & EditText


public class FragmentST extends Fragment {
    private final static String TAG = FragmentST.class.getSimpleName();
    private FloatingActionButton addP, searchP;
    private FirebaseFirestore firestore;
    String editName, editNumber;
    EditText textN, textID, searchText;
    //https://www.youtube.com/watch?v=b_tz8kbFUsU&ab_channel=TVACStudio
    public RecyclerView mResultList;
    public FirestoreRecyclerAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_st, container, false);

        firestore = FirebaseFirestore.getInstance();
        addP = (FloatingActionButton) view.findViewById(R.id.addP);
        searchP = (FloatingActionButton) view.findViewById(R.id.searchP);
        mResultList = (RecyclerView) view.findViewById(R.id.patientResults);
        searchText = (EditText) view.findViewById(R.id.search_field);
        setPatientView();
        addPatientOnClick();
        search();

        return view;
    }

    private class PViewHolder extends RecyclerView.ViewHolder {

        private TextView List_name, List_cpr;
        private ImageView icon;

        public PViewHolder(@NonNull View itemView) {
            super(itemView);
            List_name = itemView.findViewById(R.id.Pname);
            List_cpr = itemView.findViewById(R.id.Pcpr);
            icon = itemView.findViewById(R.id.pb);
        }
    }

    private void setPatientView() {
        //Query -->https://www.youtube.com/watch?v=cBwaJYocb9I&ab_channel=TVACStudio
        Query query = firestore.collection("patients");
        Log.d(TAG, "setPatientView: " + query);
        //RecyclerOptions
        FirestoreRecyclerOptions<users> options = new FirestoreRecyclerOptions.Builder<users>()
                .setQuery(query, users.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<users, PViewHolder>(options) {
            @NonNull
            @Override
            public PViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_patients_layout, parent, false);
                return new PViewHolder(view1);
            }

            @Override
            protected void onBindViewHolder(@NonNull PViewHolder holder, int position, @NonNull users model) {
                holder.List_name.setText(model.getName());
                holder.List_cpr.setText("CPR: " + model.getCpr());
                System.out.println("last character: " + model.getCpr().substring(model.getCpr().length() - 1));
                if ((Integer.parseInt(model.getCpr().substring(model.getCpr().length() - 1)) % 2) == 0) {
                    // number is even
                    int id = getResources().getIdentifier("com.example.wrd:drawable/female", null, null);
                    holder.icon.setImageResource(id);
                } else {
                    // number is odd
                    int id = getResources().getIdentifier("com.example.wrd:drawable/male", null, null);
                    holder.icon.setImageResource(id);
                }
            }
        };
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mResultList.setAdapter(adapter);
    }

    private void search() {
        searchP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "HELLO "+searchText.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void addPatientOnClick() {
        addP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Button Clicked", Toast.LENGTH_SHORT).show();
                //https://stackoverflow.com/questions/23669296/create-a-alertdialog-in-android-with-custom-xml-view
                // custom dialog
                final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
                dialog.setContentView(R.layout.fragment_add_patient);
                dialog.setTitle("Add patient");

                // set the custom dialog components - text, button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Name");
                TextView text2 = (TextView) dialog.findViewById(R.id.text2);
                text2.setText("ID");

                Button dialogButtonOk = (Button) dialog.findViewById(R.id.dialogButtonOK);
                Button dialogButtonCancel = (Button) dialog.findViewById(R.id.dialogButtonCancel);
                // if button is clicked, close the custom dialog
                dialogButtonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textN = (EditText) dialog.findViewById(R.id.editName);
                        textID = (EditText) dialog.findViewById(R.id.editNumber);
                        editName = textN.getText().toString();
                        editNumber = textID.getText().toString();
                        editNumber.length();
                        if (editNumber != null && editNumber.length() > 9) {
                            Log.d(TAG, "dialog: \ncpr: " + editNumber);
                        } else {
                            return;
                        }
                        if (editName.matches("")) {
                            return;
                        } else {
                            Log.d(TAG, "dialog: \n Name: " + editName);
                        }
                        DocumentReference documentReference = firestore.collection("patients").document(editNumber);
                        Map<String, Object> patient = new HashMap<>();
                        patient.put("name", editName);
                        patient.put("cpr", editNumber);
                        documentReference.set(patient).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                dialog.dismiss();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                            }
                        });

                    }
                });

                dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

In the XML the EditText is set to the following

    <EditText
        android:id="@+id/search_field"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/search_layout"
        android:ems="10"
        android:hint="Search"
        android:inputType="textPersonName"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:textSize="16sp"
        android:layout_weight="1"
        app:layout_constraintBottom_toBottomOf="@+id/searchP"
        app:layout_constraintEnd_toStartOf="@+id/searchP"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/searchP" />
Me NoLonely
  • 167
  • 11

0 Answers0