2013-04-11 50 views
14
<TextView 
      android:id="@android:id/empty" 
      style="@style/FacebookFont" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="@string/no_result_found" 
      android:textColor="@color/white" /> 

đây là mã xml cho chế độ xem trống. Tôi đang đặt chế độ xem trống như sau:Android: đặt chế độ xem trống thành chế độ xem danh sách

list.setAdapter(adapter); 
    list.setEmptyView(findViewById(android.R.id.empty)); 

Tôi có các mục trong listview thậm chí sau đó trước khi listview được điền chế độ xem trống được hiển thị. Tôi không muốn hiển thị chế độ xem trống khi danh sách xem chứa một số mục.

Vui lòng trợ giúp.

Trả lời

14

nếu bạn đang sử dụng ListActivity thì không cần đặt chế độ xem trống theo cách thủ công. Xóa list.setEmptyView(findViewById(android.R.id.empty)); khỏi mã của bạn.

Thay đổi bố cục như dưới đây sau đó nó tự động thêm EmptyView nếu danh sách không có mục:

<?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" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 

    <ListView android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

    <TextView android:id="@android:id/empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:text="No data"/> 

</LinearLayout> 
+0

Vẫn còn vấn đề tương tự. – user2159624

+0

Bạn có thể chỉ cho tôi mã hoạt động đầy đủ và mã tệp bố cục của mình không ??? Thêm tất cả các câu hỏi này vào câu hỏi của bạn –

+0

chắc chắn, nhưng làm cách nào để bạn giữ được TextView đó (hoặc bất kỳ chế độ xem nào) nếu bạn cần thực hiện điều gì đó, không có id thực liên quan đến nó, bạn duyệt qua bố cục theo chương trình? – Gubatron

11

Bạn cần phải thiết lập các quan điểm có sản phẩm nào trước khi thiết lập adapter, nếu bạn MainActivity không mở rộng ListActivity.

Hãy thử điều này

list.setEmptyView(findViewById(android.R.id.empty)); 
list.setAdapter(adapter); 

Xin lỗi vì sự chậm câu trả lời!

Phương pháp thay thế: Làm cho hoạt động của bạn mở rộng từ ListActivity. Và thêm một textview để bố trí danh sách của bạn với id android.R.id.empty

5

Hãy thử điều này

View view = getLayoutInflater() .inflate(<resource id of the empty view>, null); 
    ViewGroup viewGroup= (ViewGroup)list.getParent(); 
    viewGroup.addView(view); 
    list.setEmptyView(view); 
    list.setAdapter(adapter); 
5

Có một số câu trả lời đúng, tuy nhiên, tôi nghĩ rằng đây là cách tiếp cận tốt nhất mà tôi đã tìm thấy:

View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null); 
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView 
listView.setEmptyView(emptyView); 
listView.setAdapter(adapter); 
0

Bất cứ điều gì @dhawalSodhaParmar đã giải thích là chính xác. Nhưng có một sự nhầm lẫn khi bạn nhìn vào cách câu trả lời được đặt xuống.

<?xml version="1.0" encoding="utf-8"?> 

<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" 
    tools:context=".SampleActivity"> 

    <ListView 
     android:id="@+id/list" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:divider="@null" 
     android:dividerHeight="0dp" /> 

    <!-- Empty view is only visible when the list has no items. --> 
    <TextView 
     android:id="@+id/empty_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="NO DOCUMENTS AVAILABLE!" 
     android:textColor="#525252" 
     android:textAppearance="?android:textAppearanceMedium" 
     android:visibility="gone"/> 

</RelativeLayout> 

Giả sử nếu bạn đã có chế độ xem danh sách và bộ điều hợp sao lưu cùng với dữ liệu. Và giả sử mọi thứ đều hoạt động tốt, bất cứ ai muốn triển khai màn hình xem danh sách trống này phải bắt đầu như sau: Bước 1: Sửa đổi tệp XML hoạt động hiện có có chế độ xem danh sách với chế độ xem tôi đã sử dụng ở đây. Bạn có thể sử dụng Bố cục tuyến tính hoặc Bố cục tương đối, nhưng tốt nhất là Tương đối vì tính linh hoạt. Và một textView như tôi đã viết.

Bước 2: Tới tập tin Java hoạt động của bạn, và sau setContentView bạn

ListView emptyListView = (ListView) findViewById(R.id.list); 
// set your adapter here 
// set your click listener here 
// or whatever else 
emptyListView.setEmptyView(findViewById(R.id.empty_text_view)); 

Vậy là xong, bây giờ chạy và bạn đang thiết lập!

Các vấn đề liên quan