Listing 9.20 page 188


#1

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.


#2

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


#3

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


#4

[code]package com.bignerdranch.android.criminalintent;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
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;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	getActivity().setTitle(R.string.crimes_title);
	
	mCrimes = CrimeLab.get(getActivity()).getCrimes();
	CrimeAdapter adapter = new CrimeAdapter(mCrimes);
	setListAdapter(adapter);
	
}

@Override
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);
	//startActivity(i);
}



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


@Override
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)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.getDate().toString());
	CheckBox solvedCheckBox =
			(CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
	solvedCheckBox.setChecked(c.isSolved());
	return convertView;
	
}
}

}

[/code]


#5

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?


#6

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.


#7

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?