2011-01-30 5 views
1

사용자 지정 SimpleCursorAdapter를 사용하고 있습니다. 현재 데이터베이스/커서에서 항목을 찾을 수없는 경우 내 목록은 비어 있습니다. 이제 커서/데이터베이스 내에 항목이 없으면 목록에 메시지를 표시하려고합니다. 이 사건을 어떻게 처리 할 수 ​​있습니까?빈 커서 처리 SimpleCursorAdapter

답변

3

당신의 ListView가 XML로 표현하면, 특정 안드로이드와 텍스트 뷰 추가 할 수 있습니다 : 목록 아래로, 비어있는 경우 자동으로 메시지를 표시합니다 ID :

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

     <TextView android:id="@id/android:empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/white" 
      android:text="No Data exist" 
      android:textSize="16dip" 
      android:textColor="@color/black" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_horizontal"/> 

당신은을 사용해야합니다 @ id/android :이 기능이 작동하려면 비어 있습니다.

+0

죄송합니다. 언급 한 것을 잊어 버렸습니다.이 가능성은 알지만 활동은 일반적인 '활동'(ListActivity 없음)이며 임베디드 'ListView'가 있습니다. –

5

그냥 당신이 당신의 XML에 표시 할 뷰를 배치하고, 당신이 원하는 아이디, 예를 들면 제공 :에()를 호출,을 setEmptyView 당신의 활동의 에서 onCreate에서 다음

<ListView 
     android:id="@+id/myList" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <!-- view to be shown/hidden on empty list above --> 
    <TextView 
     android:id="@+id/emptyListElem" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"    
     android:gravity="center" 
     android:text="Nothing to show!" /> 

() 방법 listView 객체. ListView는 목록이 비 었는지 여부에 따라 뷰 객체 표시/숨기기를 처리해야합니다.

View empty = findViewById(R.id.emptyListElem);  
    listView.setEmptyView(empty);