user3591436 user3591436 - 5 months ago 51
Java Question

Android adapter context

New to Android dev and trying to get Context in my application adapter but I keep getting null exceptions. My attempt it to load the url string into glide, but

glide.with(context)
isn't working. The application pulls values from sqlite (this part works fine), I'm now trying to get one of those values (url) and set it to the imageview via glide. The XML has all the views required.

error: You cannot start a load on a null Context at com.bumptech.glide.manager.RequestManagerRetriever.get

UsersAdapter.java

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;

public UsersAdapter(ArrayList<User> mDataSet) {
this.mDataSet = mDataSet;
}

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_favorites2, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v);
return userViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {

holder.name_entry.setText(mDataSet.get(position).getName());
holder.email_entry.setText(mDataSet.get(position).getEmail());
holder.icon_entry.setText(""+mDataSet.get(position).getName().charAt(0));

String name = mDataSet.get(position).getName();
String url = mDataSet.get(position).getEmail();

// load image into imageview using glide
Glide.with(context)
.load(url)
.asBitmap()
.placeholder(R.drawable.ic_blank)
.error(R.drawable.ic_blank)
.into(holder.ivFavorites);
}

@Override
public int getItemCount() {
return mDataSet.size();
}

public class UserViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView name_entry, email_entry, icon_entry;
ImageView ivFavorites;

UserViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.user_layout);
name_entry = (TextView) itemView.findViewById(R.id.name_entry);
email_entry = (TextView) itemView.findViewById(R.id.email_entry);
icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
ivFavorites = (ImageView) itemView.findViewById(R.id.ivFavorites);
}
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}


Reading about adapters and Context I tried the following but with minimal success.

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;
private Context context;
public UsersAdapter(ArrayList<User> mDataSet) {
this.mDataSet = mDataSet;
}
public UsersAdapter(ArrayList<User> mDataSet, Context context) {
this.context = context;
}


My Favorites.java activity which pulls the values from sqlite and displays them in cardview (this also works fine, although I can't get glide to load the image):

public class Favorites extends AppCompatActivity {

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private UsersAdapter adapter;
private DBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
recyclerView = (RecyclerView) findViewById(R.id.usersList);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
dbHandler = new DBHandler(Favorites.this);

Log.d("Reading: ", "reading contacts..");
ArrayList<User> users = dbHandler.getAllUsers();
adapter = new UsersAdapter(users);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}


}

Answer

Pass your context when initializing your adapter

adapter = new UsersAdapter(users,getApplicationContext());

and in your adapter, implement the equivalent constructor

public UsersAdapter(ArrayList<User> mDataSet, Context context) {
    this.context = context;
    this.mDataSet = mDataSet;
}
Comments