【问题标题】:Android dialog showing empty ListView显示空 ListView 的 Android 对话框
【发布时间】:2018-01-31 18:04:27
【问题描述】:

我想在 Android 对话框中显示汽车列表,但它一直没有显示任何内容。 注意:purchasedCars.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();
        }
    });

【问题讨论】:

    标签: android listview dialog android-arrayadapter


    【解决方案1】:

    检查你的膨胀布局,它看起来像这样

    <?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() 返回什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多