I'd like to see a list view with simple_list_item_2 in the alert dialog like the title, but I get an error message when I set the list view in the builder instance and run it.Why?
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
How to customize the dialog is rolling online, but I am troubled because I cannot find my "read the data from realm and add simple_list_item_2 to the list of inclusions before displaying it in the dialog".(I don't think it's about data this time.)
Also, I referred to the following sites.
How to set both lines of a ListView using simple_list_item_2?- Stack Overflow
ListView in dialog - Qiita
Here's the code:
private void showExistingWordAlertDialog (final Word newWord, RealmResults <Word > existingWords) {
// Configuring Custom Views
LayoutInflater inflater=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
finalView listLayout=inflater.inflate(
R.layout.alert_dialog_existing_custom,
(ViewGroup) findViewById (R.id.dialogExistingCustom);
final List<Word>existingWordsList=mRealm.copyFromRealm(existingWords);
ListView listView=listLayout.findViewById (R.id.listView2);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_2,
android.R.id.text1){
@ Override
@NonNull
publicView getView(int position, View convertView, @NonNullViewGroupparent){
View view=super.getView(position, convertView, parent);
TextView text1 = view.findViewById (android.R.id.text1);
TextView text2 = view.findViewById (android.R.id.text2);
text1.setText(existingWordsList.get(position).getWord());
text2.setText(existingWordsList.get(position).getMeaning());
return view;
}
};
listView.setAdapter(adapter);
// Generate alert dialogs
mBuilder = new AlertDialog.Builder(this);
mBuilder.setView (listView) // Add this to get an error
.setTitle("Found the same word you are trying to register.")
.setPositiveButton("Registration", newDialogInterface.OnClickListener(){
@ Override
public void onClick (DialogInterface dialog, int which) {
mRealm.beginTransaction();
mRealm.copyToRealmOrUpdate(newWord);
mRealm.commitTransaction();
}
})
.setNegotisticalButton("Cancel", null);
mBuilder.create();
mBuilder.show();
}
The problem is an error caused by trying to add a View (listView
) that has already been added as a child of one View to another View (in this case, Dialog View).
If there is no particular reason why only ListView is taken out of the parent layout and set it with setView
, I think you can set it for each parent layout as follows:
//Layout inflate
LayoutInflater inflater=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
finalView listLayout=inflater.inflate(R.layout.alert_dialog_existing_custom, null);
...
// Configuring ListView and Adapter
ListView listView=listLayout.findViewById (R.id.listView2);
ArrayAdapter adapter=...;
listView.setAdapter(adapter);
// Generate AlertDialog
mBuilder = new AlertDialog.Builder(this);
mBuilder.setView (listLayout)
.setTitle("Found the same word you are trying to register.")
...
If you really want to take out only the ListView part and use it, try to define a layout xml with ListView as the root element and inflate it.
© 2024 OneMinuteCode. All rights reserved.