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
Josh Gold
12,207 PointsCannot resolve symbol UserAdapter -> Using a Custom Adapter for a Gridview Lesson
Lesson: Using a Custom Adapter for a Gridview Lesson<br>
Error message: Cannot resolve symbol UserAdapter<br>
File: FriendsFragment.java<br>
Details: Android Studio is not recognizing the UserAdapter type in my variable declaration UserAdapter adapter on line 77<br>
Code: ```java
package com.joshbgold.ribbit.ui;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.joshbgold.ribbit.R;
import com.joshbgold.ribbit.utils.ParseConstants;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import java.util.List;
public class FriendsFragment extends Fragment {
public static final String TAG = FriendsFragment.class.getSimpleName();
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected List<ParseUser> mFriends;
protected GridView mGridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_friends,
container, false);
mGridView= (GridView)rootView.findViewById(R.id.friendsGrid);
TextView emptyTextView = (TextView)rootView.findViewById(R.id.empty);
mGridView.setEmptyView(emptyTextView);
return rootView;
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
getActivity().setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
getActivity().setProgressBarIndeterminateVisibility(false);
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser friend : mFriends) {
usernames[i] = friend.getUsername();
i++;
}
if (mGridView.getAdapter()== null) {
UserAdapter adapter = new UserAdapter(getActivity(), mFriends);
mGridView.setAdapter(adapter);
}
else{
((UserAdapter)mGridView.getAdapter()).refill(mFriends);
}
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
Jon Kussmann
Courses Plus Student 7,254 PointsTry Build -> Clean Project, Build -> Rebuild Project. If you haven't tried doing so, restart Android Studio.
Josh Gold
12,207 PointsEven after copying the FriendsFragment class straight out of the download I have the same error. So I am wondering if it is not in the class itself, or the imports, but elsewhere in android studio such the dependencies in build.gradle. Do I need to add a line to include 'com.android.support:appcompat-v4.....'?
Jon Kussmann
Courses Plus Student 7,254 PointsIs your UserAdapter a RecyclerView.Adapter or another sort of adapter? It could be that you need the import for RecyclerView in your build.gradle file if that's what you're using.
When you highlight UserAdapter and press ALT + Enter, what shows up?
Josh Gold
12,207 PointsHey Jon, I feel a bit embarrassed but finally took a look at the UserAdapter.java class thanks to your persistence on this, and found the problem there. I am posting answer here in a minute. Very grateful for your questioning and troubleshooting that allowed us to solve this together!
2 Answers
Josh Gold
12,207 PointsThis was a case error. Changed file userAdapter.java from first example to second example below:
```
public class userAdapter extends ArrayAdapter<ParseUser> {
protected Context mContext;
protected List<ParseUser> mUsers;
public userAdapter(Context context, List<ParseUser> users) {
super(context, R.layout.message_item, users);
mContext = context;
mUsers = users;
}
public class UserAdapter extends ArrayAdapter<ParseUser> {
protected Context mContext;
protected List<ParseUser> mUsers;
public UserAdapter(Context context, List<ParseUser> users) {
super(context, R.layout.message_item, users);
mContext = context;
mUsers = users;
}
Jon Kussmann
Courses Plus Student 7,254 PointsHi Josh,
It doesn't look like your custom adapter wasn't imported. Could you try adding the import statement for it?
Josh Gold
12,207 PointsIHello Jon, Interesting that this happened as all the import statements previously in Android studio have been automatic... So I am guessing then the import statement will need to look like this:
import com.joshbgold..ribbit.adapters.UserAdapter;
I will try out tomorrow, thanks!
Josh Gold
12,207 PointsBoth import com.joshbgold..ribbit.adapters.UserAdapter and import com.joshbgold..ribbit.adapters.* are grayed out, meaning that are not required or used later in the class.
Jon Kussmann
Courses Plus Student 7,254 PointsIt looks like you have two periods between joshbgold and ribbit. Is that intentional?
Josh Gold
12,207 PointsRestarted android studio, clean project, rebuild project... still have same issue.
Josh Gold
12,207 PointsJosh Gold
12,207 PointsHello Jon, While I have two periods here on the forums in the import statement, the syntax I have in FriendsFragment.java is actually correct.