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

Android

Cannot resolve symbol 'parent'

I am currently working on the Adding methods for a RecyclerView Adapter lesson, specifically on the onCreateViewHolder method. When I am using the LayoutInflater to construct the View, I am seemingly unable to use 'parent' to get the Context.

Please let me know if you need anymore information and thank you for the help!

EDIT: Adding my Code (Sorry about that!)

package com.stevenbuchko.stormy.adapters;

import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;

import com.stevenbuchko.stormy.R; import com.stevenbuchko.stormy.weather.Hour;

import org.w3c.dom.Text;

/**

  • Created by stevenbuchko on 10/28/15. */ public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {

    private Hour[] mHours;

    public HourAdapter(Hour[] hours) { mHours = hours; }

    @Override public HourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.hourly_list_item, parent, false); HourViewHolder viewHolder = new HourViewHolder(view); return viewHolder; }

    @Override public void onBindViewHolder(HourViewHolder hourViewHolder, int i) { hourViewHolder.bindHour(mHours[i]); }

    @Override public int getItemCount() { return mHours.length; }

    public class HourViewHolder extends RecyclerView.ViewHolder {

    public TextView mTimeLabel;
    public TextView mSummaryLabel;
    public TextView mTemperatureLabel;
    public ImageView mIconImageView;
    
    public HourViewHolder(View itemView) {
        super(itemView);
    
        mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
        mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
        mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureLabel);
        mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
    }
    
    public void bindHour(Hour hour) {
        mTimeLabel.setText(hour.getHour());
        mSummaryLabel.setText(hour.getSummary());
        mTemperatureLabel.setText(hour.getTemperature() + "");
        mIconImageView.setImageResource(hour.getIconId());
    }
    

    }

}

2 Answers

Replace "parent" with "viewGroup" and your code will work.

Thank you! Do I need to do that because I am using a different version of something or did I configure something differently in another part of the code?

It's been a while since I saw the video in question, but I'm pretty certain that if it automatically said "parent" in the video, it's because they used an older version of Android Studio. Sometimes, the variable names change to something that makes more sense with new versions of Android Studio (as new features get added et al.), while the variable type stays the same (meaning old code still works). These small changes in naming conventions happen every once in a while. Ultimately, the developer can of course change the variable names to whatever he or she wants, as long as he or she stays consistent.

Please post your code. "parent" is not a built-in variable, I'm fairly certain, so it must be created manually.

Added code. I may have missed it in the video, but what should I declare "parent" as? An Activity?