I'm trying to make an RecycleView DialogFragment on EditText Click, but i have some problem with that: I can't implement Recycler. I have something like that:
DialogFragment class:
public class PatientDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_fragment, null));
builder.setTitle("Choose Patient");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.dialog_fragment, container, false);
final RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewPatientsDialog);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setHasFixedSize(true);
final PatientsAdapter adapter = new PatientsAdapter();
recyclerView.setAdapter(adapter);
return root;
}
}
Dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewPatientsDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="@layout/person_item"
android:layout_marginTop="50dp"
/>
</LinearLayout>
and main fragment editText listener:
editTextPatient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PatientDialogFragment patientDialogFragment= new PatientDialogFragment();
patientDialogFragment.show(getActivity().getSupportFragmentManager(), "myTag");
}
});
I bet that there is some problem with DialogFragment class, but don't know what exactly. Thanks in advance!
In this case, you should put all code in onCreateDialog
method, like this.
public class PatientDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Context context = requireActivity();
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.dialog_fragment, null);
RecyclerView recyclerView = dialogView.findViewById(R.id.recyclerViewPatientsDialog);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);
PatientsAdapter adapter = new PatientsAdapter();
// Create patient list here to pass into PatientsAdapter
// This demo I just create some random patients, replace on your own version.
List<Patient> patientList = new ArrayList<>();
patientList.add(new Patient("Alice", "Liddel", System.currentTimeMillis(), "note"));
patientList.add(new Patient("Bob", "Martin", System.currentTimeMillis(), "note2"));
patientList.add(new Patient("Clark", "Ken", System.currentTimeMillis(), "note3"));
adapter.setPatients(patientList);
recyclerView.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(dialogView)
.setTitle("Choose Patient")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
Update: Your dialog is still empty because you do not set any data (patients) for your PatientsAdapter
yet, please see my code above for more information.