I want to add my custom items, such as Spinner, in Dialog dynamically, but it not getting displayed. I suppose the spinner item is not getting accessed by findviewbyid()
.
I am not getting any error but items are not getting displayed dynamycally.
Please suggest a method to do so.
Here is my class and xml file:
public class AlertDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
Spinner spin;
spin = (Spinner)view.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Material 1");
list.add("Material 2");
list.add("Material 3");
list.add("Material 4");
list.add("Material 5");
list.add("Material 6");
//Second List
List<String> list2 = new ArrayList<String>();
list2.add("Mat 7");
list2.add("Mat 8");
list2.add("Mat 9");
//Combined List
List<String> listCombine = new ArrayList<String>();
listCombine.addAll(list);
listCombine.addAll(list2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, listCombine);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter);
builder
.setTitle("Title")
.setView(inflater.inflate(R.layout.dialog, null))
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
My xml file is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Materials"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="0dip"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="#FFB2B2"
/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:layout_marginRight="35dp"
android:textSize="20sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="55"
android:textSize="20sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Instead of inflating a second layout here:
builder
.setTitle("Title")
.setView(inflater.inflate(R.layout.dialog, null)) // Second layout!
Simply use the layout that you already inflated and set up:
builder
.setTitle("Title")
.setView(view) // Existing layout
来源:https://stackoverflow.com/questions/14676956/adding-spinner-dynamically-in-dialogfragment