【问题标题】:Android displaying text when ListView is emptyListView为空时Android显示文本
【发布时间】:2012-04-04 18:24:14
【问题描述】:

我正在设置一个 ID 为 @android:id/emptyTextView,以便在 ListView 中没有项目时显示一条消息。但是,即使 ListView 中有项目,也会在项目出现之前显示此 TextView

我怎样才能使它仅在ListView 中没有元素时才显示?

<?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"
    android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:dividerHeight="1dp" >
</ListView>

<TextView 
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empty_list" />

</LinearLayout>

PS:我正在使用 LoaderSimpleCursorAdapterListFragment

【问题讨论】:

  • 刚开始设置 textview 可见性!如果列表视图有一些数据,设置可见性可见并以编程方式设置一些文本

标签: android android-listview


【解决方案1】:

我猜你使用的是普通的FragmentActivity,里面有一个ListView。如果是,则必须手动将空布局添加到 ListView

例如

ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);

那么你的ListView会在它的适配器为空时自动使用这个视图

如果您使用的是ListActivity,则无需在ListView 上调用setEmptyView(),因为ListActivity 会自动为您管理。

【讨论】:

  • 我尝试了您发布的代码,现在它可以工作了。我正在使用 ListFragment,你知道我为什么需要添加额外的代码吗?谢谢。
  • 是的,在 ListFragment 中而不是使用空视图,您应该调用 setEmptyText("empty listview");我不相信 ListFragment 支持自动设置空视图。
  • 当我添加它时,我得到了这个异常“java.lang.IllegalStateException:指定的孩子已经有一个父母。你必须先在孩子的父母上调用removeView()。”任何线索我做错了什么?
  • @nizam.sp - 如果没有看到您的代码/xml 文件,很难判断。您可能想在 Google 上搜索问题,如果找不到解决方案,请在 stackoverflow 上发布新问题
  • 在我的情况下,ListFragment 在 API 16+ 中自己添加了空视图,但在 API14 中没有,所以我必须自己这样做:-/
【解决方案2】:

设置TextView 并在ListView 为空时为其分配您想要显示的任何内容:

ProjectListAdapter projectListAdapter = new ProjectListAdapter();
TextView empty=(TextView)findViewById(R.id.empty);
projectsListView.setEmptyView(empty);

在我的 xml 文件中,我们编写以下代码

<TextView
      android:id="@+id/empty"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:text="Your text here"
      android:textColor="#FFFFFF" />

【讨论】:

    【解决方案3】:

    我遇到了这个问题。您必须在代码中明确设置 emptyView。

    更改您的TextView

    <TextView 
        android:id="@+id/emptyResults"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty_list" />
    

    然后在onCreate()

    listViewResults = (ListView) findViewById(R.id.list);
    listViewResults.setEmptyView((LinearLayout) findViewById(R.id.emptyResults)); 
    

    以上代码假定您的 ListView 位于 LinearLayout 中。

    【讨论】:

      【解决方案4】:

      我使用了ListFragment 并遇到了同样的问题。我尝试了这个答案的所有变体,但问题没有解决。

      所以我找到了我的变体,覆盖setEmptyText()

      public class NewsFragment extends ListFragment{
          private TextView emptyText;
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
              //...
      
              emptyText = (TextView)view.findViewById(android.R.id.empty);
      
              //...
          }
      
          @Override
          public void setEmptyText(CharSequence text) {
              emptyText.setText(text);
          }
      }
      

      希望对某人有所帮助。

      【讨论】:

        【解决方案5】:

        我知道这有点晚了,但要让它在 XML 中工作,你需要重视你的 ListView 并让你的 TextView match_parent

        <?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"
            android:orientation="vertical" >
        
        <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:dividerHeight="1dp" >
        </ListView>
        
        <TextView 
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/empty_list" />
        
        </LinearLayout>
        

        【讨论】:

          【解决方案6】:

          有一个good example 说明如何操作,非常棒:

          当你想在ListView 是 空,你要记住以下3个步骤:

          • 在声明ListView 的xml 中,创建一个TextView(如果需要,TextView 可以在LinearLayout 内)对 低于ListView
          • TextView 的 id 设置为“emptyElement”
          • 在活动内部,将setEmptyView() 属性设置为ListView

          1- 创建一个包含 ListView 的 xml 并将其命名 “我的活动” 和一个名为“MyActivity”的活动。

          1. 现在,在刚刚创建的 xml “my_activity”中,您必须设置 ListView。在ListView 正下方,您必须添加 TextView。这将用于显示空消息。

          重要提示:TextView 必须具有以下名称:“emptyElement”。此名称是强制性的。消息不会显示 如果您使用其他名称。

          “my_activity”xml 应该是这样的:

          <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin"
              tools:context=".MyActivity">
          
              <ListView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@+id/listView"/>
          
              <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/emptyElement"
                  android:text="The list is empty"
                  android:textStyle="bold"
                  android:textSize="15sp"
                  android:visibility="gone"
                  android:layout_centerInParent="true"
                    android:textColor="@android:color/darker_gray"/>
          
          </RelativeLayout>
          
          1. 创建一个用于显示项目的xml(当列表不为空时),并将其命名为“list_item”。

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/list_item_text_view"
                android:textSize="20sp"
                android:padding="10dp"
                android:layout_marginLeft="5dp"/>
            

          2. 为将由 ListView 使用的自定义适配器创建一个新的 Java 类,并将其命名为“MyCustomAdapter”。适配器的代码 写在下面:

            import android.content.Context;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.BaseAdapter;
            import android.widget.TextView;
            import java.util.ArrayList;
            
            
            public class MyCustomAdapter extends BaseAdapter {
            private ArrayList<String> mListItems;
            private LayoutInflater mLayoutInflater;
            
            public MyCustomAdapter(Context context, ArrayList<String> arrayList){
            
                mListItems = arrayList;
            
                //get the layout inflater
                mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            
            @Override
            public int getCount() {
                //getCount() represents how many items are in the list
                return mListItems.size();
            }
            
            @Override
            //get the data of an item from a specific position
            //i represents the position of the item in the list
            public Object getItem(int i) {
                return null;
            }
            
            @Override
            //get the position id of the item from the list
            public long getItemId(int i) {
                return 0;
            }
            
            @Override
            
            public View getView(int position, View view, ViewGroup viewGroup) {
            
                // create a ViewHolder reference
                ViewHolder holder;
            
                //check to see if the reused view is null or not, if is not null then reuse it
                if (view == null) {
                    holder = new ViewHolder();
            
                    view = mLayoutInflater.inflate(R.layout.list_item, null);
                    holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);
            
                    // the setTag is used to store the data within this view
                    view.setTag(holder);
                } else {
                    // the getTag returns the viewHolder object set as a tag to the view
                    holder = (ViewHolder)view.getTag();
                }
            
                //get the string item from the position "position" from array list to put it on the TextView
                String stringItem = mListItems.get(position);
                if (stringItem != null) {
                    if (holder.itemName != null) {
                        //set the item name on the TextView
                        holder.itemName.setText(stringItem);
                    }
                }
            
                //this method must return the view corresponding to the data at the specified position.
                return view;
            
            }
            
            /**
             * Static class used to avoid the calling of "findViewById" every time the getView() method is called,
             * because this can impact to your application performance when your  list is too big. The class is static so it
             * cache all the things inside once it's created.
             */
            private static class ViewHolder {
            
                protected TextView itemName;
            
            }
            }
            
          3. 现在转到MyActivity 类并添加以下代码:

            import android.app.Activity;
            import android.os.Bundle;
            import android.widget.ListView; 
            import java.util.ArrayList;
            import java.util.List;
            
            
            public class MyActivity extends Activity {
            
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.my_activity);
            
                ListView listView = (ListView) findViewById(R.id.listView);
            
                // Create an empty array list of strings
                List<String> items = new ArrayList<String>();
            
                // Set the adapter
                MyCustomAdapter adapter = new MyCustomAdapter(items);
                listView.setAdapter(adapter);
            
                // Set the emptyView to the ListView
                listView.setEmptyView(findViewById(R.id.emptyElement));
             }
             }
            

          【讨论】:

            【解决方案7】:
             TextView tv=(TextView) view.findViewById(R.id.empty);
             tv.setVisibiliy(View.GONE);
            

            【讨论】:

            • 纯代码答案并不是特别有用。请简要说明此代码如何解决问题。
            猜你喜欢
            • 1970-01-01
            • 2011-04-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多