Minion
Minion

Reputation: 1064

How to get Activity reference in ListView Adapter class?

I am trying to request the phone call permission from the list adapter class on the click of a button which requires the Activity as the first argument.

 ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CALL_PHONE},1 );

here the mContext is passed from one of the activity. but it show error :

Wrong 1st argument type. Found: 'android.content.Context', required: 'android.app.Activity'.

i tried to pass every context and also the getParent() activity but it's not working. Is there any way to get the Activity and use it in list adapter ?.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                dialNumber(employee.get(i).getNumber());
                }
            else
                {

                    Log.i(TAG, "onClick: you don't have permission to call");
                    ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CALL_PHONE},1 );
                }

                Log.i(TAG, "onClick: Wroks " + getItemId(i));

        }

this is the full code for button click listener.

Thankyou.

Upvotes: 1

Views: 3462

Answers (4)

Abdullah Programmer
Abdullah Programmer

Reputation: 75

Above answer is so helpful but if context is not converting to activity and app is crashing on runtime as occurred in my case, then you can also pass activity through constructor of adapter class like this.

private Context context;
private ArrayList<String> pathsList;
private Activity activity;

public Adapter(Context context, Activity activity, ...) {
    this.context = context;
    this.activity = activity;
    ...
}

And then use this activity into your code:

ContextCompat.checkSelfPermission(activity, 
Manifest.permission.CALL_PHONE);

Upvotes: 0

Ishaan Kumar
Ishaan Kumar

Reputation: 985

Having references of Activity in Adapter class is a bad approach. Anything that should be done within the activity class such as UI changes, displaying dialog etc. should not be done anywhere else but from the activity itself.

Solution:
1) Create an interface

public interface MyListener {
     void doSomething(Params... params);
}

2) Implement it in Activity class

class MyActivity extends AppCompatActivity implements MyListener{ 
       new MyAdapter(this);  

       void doSomething(Params... params){
             //Request Permission here
       }
}

3) Use like this in Adapter class

class MyAdapter extends .....{
      MyListener myListener;

      MyAdapter(Context mContext){
           if(mContext instanceOf MyListener)
                 myListener = (MyListener) mContext;
      }

      void anotherFunction(){
             myListener.doSomething(Params... params) 
     }
}

Upvotes: 3

Bunny
Bunny

Reputation: 1064

Pass the Activity context to Adapter constructor

like this // assuming you working in activity

 ListAdapter listAdapter = new ListAdapter(this)

in your adapter

 public class ListAdapter extends BaseAdapter {
        Context context;
        LayoutInflater layoutInflater;

        public ListAdapter (Context context) {
            this.context = context;
     }

Now use the context where you want in adapter

ContextCompat.checkSelfPermission((Activity) context, Manifest.permission.CALL_PHONE);

Upvotes: 0

wgm
wgm

Reputation: 2188

If you are sure the mContext is of type Activity, just cast it like this

ContextCompat.checkSelfPermission((Activity) mContext, Manifest.permission.CALL_PHONE)

Upvotes: 1

Related Questions