Haider Akbar
Haider Akbar

Reputation: 137

Unable to get context of class in ListAdapter class

I am trying to download an image using picasso. But problem is I am not getting context of my class. I have tried to make constructor and setter getter but still not getting context. Instead getting context of MainActivity.class. Because I am using my constructor their. I am using it on a button click. You can see that in getView() method.

Here is my code

  Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT);

Upvotes: 1

Views: 937

Answers (2)

Kaaveh Mohamedi
Kaaveh Mohamedi

Reputation: 1805

In onBindViewHolder you can access context like this:

holder.itemView.context

Upvotes: 2

Ranjan
Ranjan

Reputation: 1356

Have you tried, all of the below things while calling this class,

getActivity();
getBaseContext();
this;
"Your class".this;
getApplicationContext();

or in your HomeEventListAdapter class

 android.R.id.context

Try with these things.

Use this in your class

class HomeEventListAdapter extends BaseAdapter {

   Context context;

   //constructor
   public HomeEventListAdapter(Context context){
     this.context=context;
   }
}

While calling HomeEventListAdapter from an activity class(Which extends AppCompactActivity or Activity)

HomeEventListAdapter adapter=new HomeEventListAdapter(getApplicationContext());

if "getApplicationContext()" not works, try other things which i have given. I will give you context.

Upvotes: 1

Related Questions