2017-07-27 56 views
-3

任何人都可以幫助我,如何在對話框中添加ListView。我想在customDialog中添加ListView,但得到錯誤.. ListView沒有出現。Android - ListView裏面的對話框

+0

你收到了什麼錯誤?添加相關代碼。 – Abhi

回答

0

您可以爲對話框找到文件位置:

https://developer.android.com/guide/topics/ui/dialogs.html

在該對話框中的列表,你可以使用:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle(R.string.pick_color) 
     .setItems(R.array.colors_array, new 
    DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // The 'which' argument contains the index position 
       // of the selected item 
      } 
    }); 
return builder.create(); 
} 
3

您可以使用DialogFragment到自定義佈局添加到對話。


你的碎片將需要延長DialogFragment(在app.v4一個)類:

BlankFragment.java

public class BlankFragment extends DialogFragment { 

    private ListView listView; 

    public BlankFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_blank, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     listView = view.findViewById(R.id.f_blank_list); 

     List<String> strings = Arrays.asList("Hello", "World", "Bye"); 
     ArrayAdapter<String> arrayAdapter 
       = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, strings); 

     listView.setAdapter(arrayAdapter); 
    } 
} 

然後你可以有你的對話框中設置這裏面什麼up作爲佈局:

fragment_blank.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp" 
    tools:context="com.example.myapplication.BlankFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_blank_fragment" /> 

    <ListView 
     android:id="@+id/f_blank_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </ListView> 
</LinearLayout> 

最後,你需要顯示它從你MainActivity交易:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static final String BLANK_FRAGMENT_TAG = "FRAGMENT_TAG"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void loadFragment(View view) { 
     BlankFragment blankFragment = new BlankFragment(); 
     blankFragment.show(getSupportFragmentManager(), BLANK_FRAGMENT_TAG); 
    } 
} 

這是結果:

DialogFragment with list