ListView가 비어있을 때 빈보기 표시


135

어떤 이유로 빈보기 인 경우이 경우 TextView 는 ListView 가 비어 있지 않은 경우에도 항상 나타납니다 . 빈보기를 표시 할 때 ListView 가 자동으로 감지 한다고 생각했습니다 .

<RelativeLayout android:id="@+id/LinearLayoutAR"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <ListView android:id="@+id/ARListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>
    <ProgressBar android:id="@+id/arProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"></ProgressBar>
    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
</RelativeLayout>

빈 뷰를 올바르게 연결하려면 어떻게해야합니까?

답변:


135

다음과 같아야합니다.

<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="No Results" />

id 속성에 유의하십시오 .


75
이런 식으로 작동하려면 ListViewActivity를 사용해야합니다.
— Nathan

2
아, 내 ListView는 실제로 ViewFlipper 안에 있습니다. 목록이 비어있을 때 뷰를 표시 할 수있는 다른 방법이 있습니까?
— Sheehan Alam

11
로직을 직접 구현하고 ListViewActivity의 소스 코드를 살펴보고 어떻게 작동하는지 확인하거나 쿼리가 비어 있는지 확인하면 텍스트보기를 VISIBLE로 설정하고 그렇지 않으면 GONE으로 설정하십시오.
— Nathan Schwermann

4
사용자 정의 어댑터를 사용할 때 textview 사용이 작동하지 않습니다.
— user590849

3
@schwiz 거짓! 빈 기능을 사용하기 위해 ListViewActivity가 필요하지 않습니다.
— 버팔로


118

중요한 앱은 레이아웃에서 다음과 같이 말합니다.

<ListView android:id="@+id/listView" ... />
<TextView android:id="@+id/emptyElement" ... />

그리고 연결된 활동에서 :

this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));

GridView에서도 작동합니다 ...


4
적어도 한 시간 동안 다양한 일을 시도하고 아무것도 작동하지 않으면 (목록보기의 데이터와 함께 "빈"텍스트를 표시) ListActivity를 확장하는 대신 Activity에서 ListView를 사용할 때 효과적이었습니다. 에디 감사합니다!
— Evan R.

TextView를 숨겨야합니까?
— Harsha MV

11
@Harsha android : visibility = "gone"
— scragar 2016 년

42

위의 모든 솔루션을 시도했지만 문제를 해결하기 위해 풀 솔루션을 게시했습니다.

XML 파일 :

<RelativeLayout
    android:id="@+id/header_main_page_clist1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:paddingBottom="10dp"
    android:background="#ffffff" >

    <ListView
        android:id="@+id/lv_msglist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/divider_color"
        android:dividerHeight="1dp" />

    <TextView
        android:id="@+id/emptyElement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="NO MESSAGES AVAILABLE!"
        android:textColor="#525252"
        android:textSize="19.0sp"
        android:visibility="gone" />
</RelativeLayout>

textView ( "@ + id / emptyElement" )는 빈 목록보기의 자리 표시 자입니다.

다음은 자바 페이지 코드입니다.

lvmessage=(ListView)findViewById(R.id.lv_msglist);
lvmessage.setAdapter(adapter);
lvmessage.setEmptyView(findViewById(R.id.emptyElement));

어댑터를 listview에 바인딩 한 후 emptyView를 배치해야합니다.

산출:

여기에 이미지 설명을 입력하십시오


1
이 추가 한 후 나를 위해 일 android:layout_below="@+id/lv_msglist"을 위해 emptyElement
— Behnam Heydari을

16

이런 식으로 ViewStubs를 사용하는 것이 좋습니다.

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout="@layout/empty" />
</FrameLayout>

Cyril Mottier 의 전체 예보기


8
<ListView android:id="@+id/listView" ... />
<TextView android:id="@+id/empty" ... />
and in the linked Activity:

this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.empty));

지원 라이브러리를 사용하는 경우 FragmentActivity와 함께 명확하게 작동합니다. API 17 즉 4.2.2 이미지를 빌드하여이를 테스트했습니다.


7

활동 코드, 확장하는 것이 중요합니다 ListActivity.

package com.example.mylistactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.mylistactivity.R;

// It's important to extend ListActivity rather than Activity
public class MyListActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist);

        // shows list view
        String[] values = new String[] { "foo", "bar" };

        // shows empty view
        values = new String[] { };

        setListAdapter(new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                values));
    }
}

두보기의 id 인 레이아웃 XML이 중요합니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- the android:id is important -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <!-- the android:id is important -->
    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="i am empty"/>
</LinearLayout>

7

실제로 새 ID를 만들 필요가 없다는 것을 추가하기 위해 다음과 같은 것이 작동합니다.

레이아웃에서 :

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/empty"
    android:text="Empty"/>

그런 다음 활동에서 :

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setEmptyView(findViewById(android.R.id.empty));

4

나는이 문제가 있었다. 클래스를 Activity가 아닌 ListActivity로 확장하고 XML에서 내 목록의 이름을android:id="@android:id/list"


2

프로그래밍 방식으로 솔루션은 다음과 같습니다.

TextView textView = new TextView(context);
textView.setId(android.R.id.empty);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("No result found");
listView.setEmptyView(textView);

-3

먼저 목록에 일부 값이 포함되어 있는지 확인하십시오.

if (list.isEmpty()) {
    listview.setVisibility(View.GONE);
}

그렇다면 OK, 그렇지 않으면 다음을 사용하십시오.

else {
     listview.setVisibility(View.VISIBLE);
}

당신은 무엇을 더 정교하게 list하시겠습니까?
— Roy Lee

ArrayList <xyz>입니다
— Avnish Kumar

Object [] 응답 = Utility.parseStationresponce (객체); int statusCode = (정수) 응답 [0]; @SuppressWarnings ( "unchecked") ArrayList <Stations> list = (ArrayList <Stations>) 응답 [2];
— Kumar Avnish

글쎄, 나는 당신이 그것을 의견보다 당신의 대답의 일부로 넣는 것이 좋습니다. 이것은 다른 사람들이 당신이 쓰고있는 것에 대한 특정한 맥락을 이해하도록하는 데 중요합니다. 또한 답변을 OP의 질문과 관련시킬 수있는 것이 가장 좋습니다. 마지막으로, SO에 오신 것을 환영합니다.
— Roy Lee
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.