android ListView没有数据时进行信息提示的两种方法
通过添加一个id为android:empty的TextView,当ListView里面没有data的时候,就会显示TextView的内容。
 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<ListView android:id="@id/android:list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
<TextView android:id="@id/android:empty" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="对不起,没有数据显示"/> 
</LinearLayout> 
需要注意的是,ListView的id,使用的是"@id/android:list",即系统默认的id,而紧接其后的TextView,使用的id也是系统默认的,即"@id/android:empty"。
如果需要获取此listview,可以用ListView listView =(ListView) findViewById(android.R.id.list);
不过我在测试中发现一个问题:在有数据时,也会显示 提示信息,怎么去掉呢?
ListView listView = (ListView) findViewById(android.R.id.list);
View emptyView = findViewById(android.R.id.empty);
listView.setEmptyView(emptyView);
----------------另外一种方法----------------------
ListView是android中一个非常常用的控件,当ListView的adapter数据为空时会只是黑乎乎的一片,显得很不友好,这时我们可以通过listView.setEmptyView(view)方法来设置一些提示语,具体实现方式如下:
TextView tv=newTextView(this); tv.setText("没有查询到数据"); tv.setGravity(Gravity.CENTER); tv.setTextSize(25); //设置字体大小LayoutParams params = newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); addContentView(tv, params); pausList.setEmptyView(tv)
当然,你也可以自定义一个比较美观的View,这里只是抛砖引玉,就不去实现了。listView.setEmptyView(view)可以是一个.xml布局。
//带上下拉刷新的ListView(最好和设置Adapter的时候一起用)
View v = mInflater.inflate(R.layout.default_background, null);
  TextView  text = (TextView) v.findViewById(R.id.default_text);
  ImageView  img = (ImageView) v.findViewById(R.id.default_img);
  text.setText("@lov_syc");
  img.setBackgroundResource(R.drawable.default_recommend);
  mListView = mRefreshView.getRefreshableView();//这里是上下拉刷新的LisView  跟普通的listView一样用
  mListView.setEmptyView(v);
普通的ListView的使用:
mAdapter = new MyAfterServiceAdapter(this, listData);
  listView.setAdapter(mAdapter);
  LayoutInflater mInflater = LayoutInflater.from(this);
  View v = mInflater.inflate(R.layout.default_background, null);
  TextView  text = (TextView) v.findViewById(R.id.default_text);
  ImageView  img = (ImageView) v.findViewById(R.id.default_img);
  text.setText("您还没有预定过售后服务哦~");
  img.setBackgroundResource(R.drawable.default_recommend);
  ViewGroup parentView = (ViewGroup) listView.getParent();
  //parentView.addView(v,new LayoutParams(Gravity.CENTER));//这行代码,API兼容性有问题,低版本会报错
//没有这两步骤就没有效果,new LayoutParams(Gravity.CENTER));//不知道怎么的,xml居中无效,所以这里要设置居中
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.gravity = Gravity.CENTER;//用这两行代码设置居中
    v.setLayoutParams(params);
  listView.setEmptyView(v);
.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_bg"
    android:orientation="vertical" >
   <LinearLayout 
        android:layout_width="match_parent"
     android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:orientation="vertical"
       >
       <ImageView
            android:id="@+id/default_img" 
            android:layout_width="wrap_content"
         android:layout_height="wrap_content"
            android:src="@drawable/default_recommend"
            android:layout_margin="12dip"
           />
       <TextView
           android:id="@+id/default_text"
           android:textSize="@dimen/title_text_size"
           android:textColor="#B7B7B7"
           android:layout_width="wrap_content"
        android:layout_height="wrap_content"
           android:text="亲,记得给好评哦~"
           android:singleLine="false"
           />
   </LinearLayout>
</RelativeLayout>
                    
                
                
            
        
浙公网安备 33010602011771号