Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


CustomListViewAdapter having illegalStateException

I am working on CustomListViewAdapter currently, and here's the extract of the getView method in my code in CustomListViewAdapter.java. I think the problem lies on here, mostly probably in the line { final View view = super.getView(...) }, as I have used the debug mode for testing. How to solve it?

Description: what I want to do is, when AND only when clicked on the switchImage in the row, the corresponding switchImage and lockImage change their images. I have tried, but OnItemClickListener in my MainActivity.class doesnt allow me to specify that only when clicking switchImage would the effect appear (effect appears once clicking on ANY items in the row).

THX for any helps and comments.

public View getView(int position, View convertView, ViewGroup parent) {
    final View view = super.getView(position, convertView, parent);
    final ImageView switchImage = (ImageView) view.findViewById(R.id.onOffSwitch);
    final ImageView lockImage = (ImageView) view.findViewById(R.id.lock);
    switchImage.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (flag == false) {
                flag = true;

    RowItem rowItem = getItem(position);

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.lock = (ImageView) convertView.findViewById(R.id.lock);
        holder.appName = (TextView) convertView.findViewById(R.id.appName);
    } else {
        holder = (ViewHolder) convertView.getTag();

    return view;