Another empty list challenge post


#1

Alright, I’ve been boggled by this challenge for awhile and the other post on the forum don’t really help me.

I understand that you can define a layout and use preset ids for the views and that the list view should automatically inflate the required view accordingly whether it’s a defined list item or an empty view. However, I can not achieve this functionality.

Here is my modified layout file with the empty view in it.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <CheckBox
        	android:id="@+id/crime_list_item_solvedCheckBox"
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center"
        	android:layout_alignParentRight="true"
        	android:focusable="false"
        	android:enabled="false"
        	android:padding="4dp" />
    
    	<TextView
        	android:id="@+id/crime_list_item_titleTextView"
        	android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
        	android:textStyle="bold"
        	android:paddingLeft="4dp"
        	android:paddingRight="4dp"
        	android:text="Crime title" />
    
    	<TextView
        	android:id="@+id/crime_list_item_dateTextView"
        	android:layout_width="match_parent"
        	android:layout_height="wrap_content"
        	android:layout_below="@id/crime_list_item_titleTextView"
        	android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
        	android:paddingLeft="4dp"
        	android:paddingRight="4dp"
        	android:paddingTop="4dp"
        	android:text="Crime date" />
        
    </ListView>
    
    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="@string/crimes_empty_list" />

</FrameLayout>

Am I doing something wrong here? Or could my problem be in the ListFragment’s code?

Here is the onCreateView method of CrimeListFragment.java. It’s unmodified and doesn’t inflate the new layout however when I do try following examples posted on this board I get runtime errors.

@TargetApi(11)
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = super.onCreateView(inflater, parent, savedInstanceState);
		//View v = inflater.inflate(R.layout.list_crimes, parent); // Causes a runtime error.

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && mSubtitleVisible)
			getActivity().getActionBar().setSubtitle(R.string.subtitle);

		return v;
	}

I’ve also played with the getView method of the ArrayAdapter. Right now it’s inflating the unmodified basic list item layout without the empty view. Referencing the new layout file with the empty view causes a runtime error also.

@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// If no view, inflate one.
			if (convertView == null) 
				convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);			
			
			// Configure the view for this Crime.
			Crime c = getItem(position);
			
			TextView titleTextView = (TextView) convertView.findViewById(R.id.crime_list_item_titleTextView);
			titleTextView.setText(c.getTitle());
			
			TextView dateTextView = (TextView) convertView.findViewById(R.id.crime_list_item_dateTextView);
			dateTextView.setText(c.getFormattedDate());
			
			CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
			solvedCheckBox.setChecked(c.isSolved());			
			
			return convertView;
		}

If anyone could correct or point me in the right direction I would greatly appreciate it.


#2

Why do you have the TextViews and Checkbox inside the ListView? You already have the layout of each list item.
Update your layout and it should work.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   
    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="@string/crimes_empty_list" />

</FrameLayout>