在 BaseAdapter 中访问 getContentResolver()
Access getContentResolver() inside of a BaseAdapter
我的一个新 android 项目需要一些帮助。让我快速解释一下结构。
我的项目有一个 MainActivity,在 activity 中我有 3 个片段,可通过应用程序上的选项卡访问。在我最后的 tab/fragment 上,我试图列出 phone 上的所有联系人以及照片、姓名和电话号码。我遇到了以下名称和号码代码:
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;
}
如果我将以下代码放在我的片段中并更改某些引用的名称,如 getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
至 getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
无论如何,所以我已经能够让它工作,但是遇到了一些问题,比如在左侧列出带有图像的联系人,所以我看到了一个 YouTube 教程 Android BaseAdapter Tutorial。经过大量的面面相觑并试图弄清楚到底发生了什么(我是 Android 开发的新手)我终于完成了前 2 个教程并解决了一个小问题。
尝试创建联系人访问权限(使用此问题中的第一段代码)我发现我无法这样做,因为函数 getContentResolver()
不可访问,除非它在 [=34= 中].这是我当前的 BaseAdapter 代码:Android BaseAdapter Test Code - Paste Bin.
如果有人能指导我解决这个问题的最佳方法,那将对我有很大帮助。
附带说明一下,有没有更好的方法来访问联系人并简单地列出他们的照片、姓名和电话号码?也许是图书馆?任何超级简单的东西都可以; WhatsApp 甚至像联系人列表,还是 Viber?谢谢!
为了调用 getContentResolver()
你需要 activity 的 Context
对象,你必须在使用 BaseAdapter
class.
1.通过构造函数传递它 -
在您的适配器内:
public class MyBaseAdapter extends BaseAdapter {
private Context context;
public MyBaseAdapter (Context context) {
this.context = context;
}
// Do your stuff
}
在你的 activity 里面:
MyBaseAdapter myBaseAdapter = new MyBaseAdapter(getApplicationContext());
2。从视图中获取上下文-
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;
}
}
然后调用 context.getContentResolver()
.
我的一个新 android 项目需要一些帮助。让我快速解释一下结构。
我的项目有一个 MainActivity,在 activity 中我有 3 个片段,可通过应用程序上的选项卡访问。在我最后的 tab/fragment 上,我试图列出 phone 上的所有联系人以及照片、姓名和电话号码。我遇到了以下名称和号码代码:
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;
}
如果我将以下代码放在我的片段中并更改某些引用的名称,如 getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
至 getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
无论如何,所以我已经能够让它工作,但是遇到了一些问题,比如在左侧列出带有图像的联系人,所以我看到了一个 YouTube 教程 Android BaseAdapter Tutorial。经过大量的面面相觑并试图弄清楚到底发生了什么(我是 Android 开发的新手)我终于完成了前 2 个教程并解决了一个小问题。
尝试创建联系人访问权限(使用此问题中的第一段代码)我发现我无法这样做,因为函数 getContentResolver()
不可访问,除非它在 [=34= 中].这是我当前的 BaseAdapter 代码:Android BaseAdapter Test Code - Paste Bin.
如果有人能指导我解决这个问题的最佳方法,那将对我有很大帮助。
附带说明一下,有没有更好的方法来访问联系人并简单地列出他们的照片、姓名和电话号码?也许是图书馆?任何超级简单的东西都可以; WhatsApp 甚至像联系人列表,还是 Viber?谢谢!
为了调用 getContentResolver()
你需要 activity 的 Context
对象,你必须在使用 BaseAdapter
class.
1.通过构造函数传递它 -
在您的适配器内:
public class MyBaseAdapter extends BaseAdapter {
private Context context;
public MyBaseAdapter (Context context) {
this.context = context;
}
// Do your stuff
}
在你的 activity 里面:
MyBaseAdapter myBaseAdapter = new MyBaseAdapter(getApplicationContext());
2。从视图中获取上下文-
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;
}
}
然后调用 context.getContentResolver()
.