ListView가 비어있을 때 빈보기 표시
어떤 이유로 빈보기 인 경우이 경우 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>
빈 뷰를 올바르게 연결하려면 어떻게해야합니까?
다음과 같아야합니다.
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
id 속성에 유의하십시오 .
당신이 확장 할 때 FragmentActivity
나 Activity
와 하지 ListActivity
, 당신이보고 싶을 것이다 :
중요한 앱은 레이아웃에서 다음과 같이 말합니다.
<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에서도 작동합니다 ...
위의 모든 솔루션을 시도했지만 문제를 해결하기 위해 풀 솔루션을 게시했습니다.
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를 배치해야합니다. 처음으로 작동하지 않았고 setAdapter 후에 setEmptyView를 이동 한 후에는 작동합니다.
산출:
이런 식으로 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 의 전체 예보기
<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 이미지를 빌드하여이를 테스트했습니다.
활동 코드, 확장하는 것이 중요합니다 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>
실제로 새 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));
I had this problem. I had to make my class extend ListActivity rather than Activity, and rename my list in the XML to android:id="@android:id/list"
A programmatically solution will be:
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);
First check the list contains some values:
if (list.isEmpty()) {
listview.setVisibility(View.GONE);
}
If it is then OK, otherwise use:
else {
listview.setVisibility(View.VISIBLE);
}
참고URL : https://stackoverflow.com/questions/3771568/showing-empty-view-when-listview-is-empty
'Programing' 카테고리의 다른 글
공백을 밑줄로 바꾸기 (0) | 2020.06.29 |
---|---|
macOS High Sierra에서 작동하지 않는 CocoaPod (0) | 2020.06.29 |
HttpServletRequest에서 getRequestURI와 getPathInfo 메소드의 차이점은 무엇입니까? (0) | 2020.06.29 |
Java가 10에서 99까지의 모든 숫자의 곱이 0이라고 생각하는 이유는 무엇입니까? (0) | 2020.06.29 |
LogCat에 필터 옵션이 없습니다. (0) | 2020.06.29 |