2014-12-18 3 views
0

ArrayAdapter를 사용하여 ListFragment에 대한 사용자 정의 레이아웃을 만들었지 만 배열이 비어 있으면 빈 메시지가 표시되지 않습니다. 콘솔에 오류가 표시되지 않습니다. 내 최소 SDK 버전은 15이며 지원 라이브러리를 사용할 필요가 없습니다.사용자 정의 레이아웃이있는 ListFragment가 빈 목록 알림자를 채우지 않습니다.

headline_list.xml (즉, 내 사용자 지정 레이아웃 파일)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

    <TextView android:id="@+id/my_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

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

</LinearLayout> 

HeadlineFragment.java 그것은 선형 레이아웃과, 당신은하지 않았다 사실과 관련이있을 수 있습니다

public class HeadlineFragment extends ListFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.headline_list, container, false); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     List<String> headlines = ...;   // populate headlines list 

     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       R.layout.headline_list, R.id.my_text, headlines)); 
    } 
} 

답변

0

은 단순히 텍스트를 설정하는 것을 잊었다했다 !

을 설정 한 후, 내 텍스트 뷰가 올바르게 표시됩니다 :

<string name="empty_text">There are no data</string> 

불행하게도, 텍스트 뷰도 목록의 각 행에 표시됩니다 strings.xml의 파일에 문자열 리소스와

<TextView android:id="@id/android:empty" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:text="@string/empty_text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

,하지만 이것은 또 다른 질문입니다.

+0

복수를 downvote? 어서! – perissf

-1

지정된 가중치 속성을 각 항목에 적용하여 다른 항목을 오버레이 할 수 있습니다. 당신이 시도를해야한다, 당신은 여기에서 제안을 받고 Showing empty view when ListView is empty

+0

직접 해 보았습니까? 당신은 단순히 다른 사람의 대답을 연결했고, 그 답은 제 경우에는 효과가 없었습니다. – perissf

0

.. 어쨌든이 레이아웃은 완벽하게 나를 위해 노력하고 있습니다 : 귀하의 레이아웃 PARAMS이 확인

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

      ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, from); 
      setListAdapter(adapter); 

      return rootView; 
     } 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragmet" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 

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

    <!-- 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>