任何人都可以幫助我,如何在對話框中添加ListView。我想在customDialog中添加ListView,但得到錯誤.. ListView沒有出現。Android - ListView裏面的對話框
-3
A
回答
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);
}
}
這是結果:
相關問題
- 1. android查看對話框內的listview對話框
- 2. Android listview裏面的相對佈局
- 3. Android的ListView裏面的標籤佈局裏面的對話框無法獲得滾動條來顯示
- 4. jquery對話框:拖動裏面有iframe的對話框
- 5. android listview to editTxt在對話框內
- 6. android中的asynctask裏面的alert對話框
- 7. 裏面的Android對話框,如何設置onActivityResult for startActivityForResult?
- 8. 隱藏對話框從裏面LWUIT
- 9. listview裏面的listview Xamarin.Forms
- 10. 安卓:項目點擊監聽ListView的是裏面一個對話框
- 11. 裏面的ListView
- 12. ListView裏面viewflipper
- 13. ListView活動和對話框
- 14. Android對話框界面獲取內部對話框視圖
- 15. TabHost裏面的對話..?
- 16. ListView裏面的ListView項
- 17. Android對話框
- 18. listview裏面的選項卡android
- 19. Android的ListView裏面RelativeLayout技巧
- 20. 如何把對話框裏面的頁面刷新?
- 21. ScrollView裏面的ListView
- 22. ListView裏面的CollapsiblePanelExtender
- 23. ViewPager裏面ListView
- 24. Android對話框:解除對話框後我應該返回哪裏?
- 25. ListView打開一個對話框(或提示對話框)
- 26. ListView裏面的TextView:setTextHighlightColor()問題
- 27. android-在popupwindow裏面過濾listview
- 28. FILL_PARENT在ViewFlipper裏面沒有標題的對話框
- 29. 對話框中的Android ListView - 空指針異常
- 30. 對話框中的黑色屏幕ListView Xamarin Android
你收到了什麼錯誤?添加相關代碼。 – Abhi