簡體   English   中英

Android對話框顯示空的ListView

[英]Android dialog showing empty ListView

我想在Android對話框中顯示汽車列表,但它始終不顯示任何內容。 注意:PurchaseCars.getCars()不為空這是代碼:

  findViewById(R.id.buy).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(BuyCarActivity.this);
            View dialogView=getLayoutInflater().inflate(R.layout.dialog_list,null);
            builder.setView(dialogView);

            ListView listView = dialogView.findViewById(R.id.listView2);
            listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, purchasedCars.getCars()));

            builder.show();
        }
    });

檢查您的膨脹布局,它看起來像這樣一個

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</LinearLayout>

並可以測試是否更改了purchasedCars.getCars()

到-> new String[]{"test","app"}

例如

listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, new String[]{"test","app"}));

要么

 List<String> list = new ArrayList<>(); 
                list.add("test");
                list.add("app");


listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, list));

檢查多數民眾贊成在工作,如果是的話..您需要看看purchasedCars.getCars()purchasedCars.getCars()返回的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM