如果TextView
中没有项目,我设置了@android:id/empty
,其ID为ListView
,以显示消息。但是,即使在项目出现之前TextView
中有项目,也会显示此ListView
。
如何在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:我使用Loader
和SimpleCursorAdapter
ListFragment
。
答案 0 :(得分:99)
我猜你正在使用常规的Fragment
或Activity
,其中包含ListView
。如果是,则必须手动将空布局添加到ListView
。
E.g。
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
然后,ListView
将在其适配器为空时自动使用此视图
如果您使用ListActivity
,则无需致电setEmptyView()
ListView
,因为ListActivity
会自动为您管理。{/ p>
答案 1 :(得分:10)
设置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" />
答案 2 :(得分:5)
我有这个问题。您必须在代码中明确设置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
。
答案 3 :(得分:3)
我使用了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);
}
}
希望它会对某人有所帮助。
答案 4 :(得分:3)
我知道这有点晚了,但是要使用XML工作,你需要加重ListView
并让你的TextView
匹配_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>
答案 5 :(得分:3)
有good example如何做到这一点很棒:
当您想要
ListView
时向用户显示消息时 空的,你必须记住以下3个步骤:
- 在声明
ListView
的xml中,创建一个TextView
(如果需要,TextView可以在LinearLayout
内) 低于ListView
- 将
TextView
的ID设为“emptyElement”- 在活动中,将
setEmptyView()
属性设置为ListView
1-创建一个xml,它将保存
ListView
并为其命名 “my_activity” 和一项名为“MyActivity”的活动。
- 现在,在刚刚创建的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>
创建一个用于显示项目的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"/>
为自定义适配器创建一个新的Java类,ListView将使用它并命名为“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; } }
- 醇>
现在转到
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)); } }
答案 6 :(得分:0)
TextView tv=(TextView) view.findViewById(R.id.empty);
tv.setVisibiliy(View.GONE);