只需要调用在代码中调用setEmptyView(View emptyView)设置ListView为空时显示这个TextView即可。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <!-- Here is the view to show if the list is emtpy --> <TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No items." /> </LinearLayout >

实现

private ListView mListView = null;

mListView = (ListView) findViewById(R.id.myList);
mListView.setEmptyView(findViewById(R.id.myText)); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, generateStrings()); mListView.setAdapter(adapter);

扩展:

  因为setEmptyView(View emptyView)这个方法是属于AdapterView这个类的,所以除了ListView之外,其他的子类,如GridView,Spinner等,应该也可以用这个方法来设置Adapter数据为空时显示另一个View。

Android ListView无数据项时提示的更多相关文章

  1. android ListView 在初始化时多次调用getView()原因分析

    今天在做一个功能:在初始化ListView时,把第一行背景置为黄色,同时保存第一行对象,用于在点击其他行时将该行重新置为白色. if(position==0){ convertView.setBack ...

  2. Android中使用getDrawable时提示:Call requires API level 21(current min is 15)

    场景 在通过getDrawable方法获取照片资源时提示: Call requires API level 21(current min is 15) 注: 博客: https://blog.csdn ...

  3. android studio新项目时提示:Plugin is too old, please update to a more recent version

    今天想写一个程序来测试一下android studo代码,但是创建好项目后,提示: Error:(1, 0) Plugin is too old, please update to a more re ...

  4. Xamarin.Android 开发,生成时提示“Resource.Drawable”未包含“BG”的定义

    Xamarin Android提示Resource.Drawable”未包含“BG”的定义错误信息:error CS0117: '“Resource.Drawable”未包含“BG”的定义Xamari ...

  5. Android Studio运行app时提示Error: Please select Android SDK

    最近开启了一个android项目,点“Run”键运行app,并报错Error:Please select Android SDK: 选择 File -> Project Structure 调整 ...

  6. linux下使用Android studio启动模拟器时提示 waiting for target device to come online 的问题

    方法来自:http://stackoverflow.com/questions/42612468/how-can-i-get-more-information-about-waiting-for-ta ...

  7. ListView 无 DataSource 依然用 DataPager 翻页

    ListView 有 DataSource 使用 DataPager 翻页ListView 无 DataSource 使用 DataPager 翻页问题描述点击两次才能翻页返回上一页,内容为空解决方法 ...

  8. Android ListView的item背景色设置以及item点击无响应等相关问题

    Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...

  9. [原创]LoadRunner 12.02 录制脚本时提示无Internet访问,如何解决?

    在使用LoadRunner 12.02 进行录制脚本时提示无Internet访问,如下图: 翻译中文如下: 可以尝试以下方式解决:点击弹出框中的“Yes”即可. 若还是有问题,尝试以下方式: (1)L ...

随机推荐

  1. 快捷键&小技巧

    shift+鼠标滚轮:实现左右移动 alt+鼠标左键双击:打开属性 chrome中在F12下的Element中,可以先选中某一项,可以直接按住F2进行编辑 chrome中element的右下方我们可以 ...

  2. java内存数据管理

    准确的说应该是java8以前的内存管理方式 区别在永久代(方法区)上 public class RamManager { //1.a存储于永久代 public static int a =1; pri ...

  3. 【C#小知识】C#中一些易混淆概念总结(七)---------解析抽象类,抽象方法

    目录: [C#小知识]C#中一些易混淆概念总结--------数据类型存储位置,方法调用,out和ref参数的使用 [C#小知识]C#中一些易混淆概念总结(二)--------构造函数,this关键字 ...

  4. 2013-12-LINUX 常用命令

    查看iptables状态: service iptables status 查询LINUX开机时间多久 1. cat /proc/uptime输出: 105040.44 105024.75 秒 2. ...

  5. 原生js动态添加style,添加样式

    原生js动态添加style,添加样式 第一种 var style="[assign-url='"+str+"']{display:initial}"; var ...

  6. 11 - JavaSE之GUI

    GUI(念法 gu yi) AWT AWT(Abstract Window Toolkit 抽象窗口开发包,在C# 或者 linux窗口开发类之上又封装一层,达到跨平台的目的)包括了很多类和接口,用于 ...

  7. lua的克隆函数,table的深度拷贝

    --深度拷贝Table function DeepCopy(obj) local InTable = {}; local function Func(obj) if type(obj) ~= &quo ...

  8. android学习-Activity和Service的生命周期

    详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期

  9. centos7设置SSH安全策略–指定IP登陆

    之前自己搭建了个博客网站(理想三旬),写了些文章,但是由于一些原因慢慢将文章放在博客园了.所以这里将一些文章复制过来.便于以后自己查询. 为了服务器的安全性,我们在日常使用需要授予权限和指定ip登陆来 ...

  10. Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法

    什么是ApplicationContext? 它是spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...