Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access getContentResolver() inside of a BaseAdapter

I needed some help with one of my new android projects. Let me just quickly explain the structure.

My project has a MainActivity and within the activity I have 3 fragments which are accessible via the tabs on the app. On my very last tab/fragment I am trying to list all of the contacts on the phone along with a picture, name and number. I came across the following code for the name and number:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show();

            String full = name + " " + phoneNumber;


        }

The following code works like a charm if I place it within my fragment and name change certain references like getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); to getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

anyway, so I have been able to get that to work, but fell onto problems like listing the contacts with images on the left side, so I came across a YouTube tutorial Android BaseAdapter Tutorial. After a lot of facepalm moments and trying to figure out what on earth is going on (I am new to Android development) I finally got through the first 2 tutorials and landed on a small problem.

Trying to create access the contacts (using the first block of code in this question) I discovered that I cannot do so as the function getContentResolver() is not accessible unless if it is within a Activity. Here is my current code for my BaseAdapter: Android BaseAdapter Test Code - Paste Bin.

It would help me out so much if someone could direct me on the best way to approach this.

On a side note, is there any better way of accessing contacts and just simply listing them out with picture, name and number? A library maybe? Anything super simple works; WhatsApp like contact listing even, or Viber? Thanks!

like image 954
user282190 Avatar asked Jan 20 '26 19:01

user282190


1 Answers

In order to call getContentResolver() you need a Context object of your activity, you have to ways to do this when using the BaseAdapter class.

1. Pass it via constructor -
Inside your adapter:

public class MyBaseAdapter extends BaseAdapter {
    private Context context;

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

    // Do your stuff
}

Inside your activity:

MyBaseAdapter myBaseAdapter = new MyBaseAdapter(getApplicationContext());

2. Get the context from the view-

public class MyBaseAdapter extends BaseAdapter {

        //BaseAdpter stuff

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(inflater == null){
            Context context = parent.getContext();
            }
            //BaseAdpter stuff
            return convertView;
        }

}

Then just call context.getContentResolver().

like image 200
Nir Duan Avatar answered Jan 23 '26 08:01

Nir Duan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!