2013-02-03 98 views
4

我想添加我的自定義項目,例如SpinnerDialog動態地顯示,但沒有顯示。我猜想微調項目沒有被findviewbyid()訪問。 我沒有收到任何錯誤,但項目沒有得到動態顯示。在DialogFragment中動態添加微調框

請建議一個方法來做到這一點。

這是我的階級和XML文件:

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(); 
} 

}

我的XML文件是:

<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> 

回答

4

代替膨脹第二LAYO UT這裏:

builder 
    .setTitle("Title") 
    .setView(inflater.inflate(R.layout.dialog, null)) // Second layout! 

只需使用你已經膨脹,併成立了佈局:

builder 
    .setTitle("Title") 
    .setView(view) // Existing layout