Listing 9.20 page 188


in the print version the added line is
Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);
It works fine if I don’t add this line.

This is the error:
TYpe mismatch - cannot convert from CrimeListFragment.CrimeAdapter to Crime

Also, there seems to be an extra left parenthesis.

The source code in the documentation also does not use CrimeAdapter.


Hmm, getItem() should in fact return a Crime since CrimeAdapter extends ArrayAdapter. I’m not seeing the extra ‘(’ either.


Do you mean you are using an extra “(” or not using it? I assume CrimeAdapter works for you?



import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class CrimeListFragment extends ListFragment {

private static final String TAG = "CrimeListFragment";

private ArrayList<Crime> mCrimes;

public void onCreate(Bundle savedInstanceState) {
	mCrimes = CrimeLab.get(getActivity()).getCrimes();
	CrimeAdapter adapter = new CrimeAdapter(mCrimes);

public void onListItemClick(ListView l, View v, int position, long id) {
	Crime c = ((CrimeAdapter)(getListAdapter()).getItem(position);
	//Log.d(TAG, c.getTitle() + "was clicked");
	//Listing 10.1 has same issue as 9.20
	//start CrimeActivity
	//Intent i = new Intent(getActivity)(), CrimeActivity.class);

private class CrimeAdapter extends ArrayAdapter<Crime> {
	public CrimeAdapter(ArrayList<Crime> crimes) {
		super(getActivity(), 0, crimes);

public View getView(int position, View convertView, ViewGroup parent) {
	//if we weren't given a 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 dateTextView =
	CheckBox solvedCheckBox =
	return convertView;




So this is the code that generates the type mismatch error for CrimeAdapter.

Update - still plodding through but I have not resolved my confusion. Is this forum being monitored by admins?


I copied and pasted your code into my project. I got two errors: “Syntax error, insert ‘)’…” and “Type mismatch: cannot convert from CrimeListFragment.CrimeAdapter to Crime.”

Both errors pointed to this line:

I changed that line to this:

After the change, both errors disappeared.


Thanks! And that exactly explains why I got listing 10.8 to run. Still doesn’t explain the source code though…
I’ll have to go back through and see how it carries through the rest of the code.

Looks like I’ve been going blind, doesn’t it?