Android 将联系人提取到回收器视图的速度太慢
我正在将联系人提取到回收器视图。。。现在,下面的代码非常有效,但获取联系人需要花费太多时间。。。。因此,如果有任何方法可以快速获得联系,请指导我Android 将联系人提取到回收器视图的速度太慢,android,Android,我正在将联系人提取到回收器视图。。。现在,下面的代码非常有效,但获取联系人需要花费太多时间。。。。因此,如果有任何方法可以快速获得联系,请指导我 public class AllContactsAdapter extends RecyclerView.Adapter<AllContactsAdapter.ContactViewHolder>{ private List<ContactVO> contactVOList; private Context mContext;
public class AllContactsAdapter extends RecyclerView.Adapter<AllContactsAdapter.ContactViewHolder>{
private List<ContactVO> contactVOList;
private Context mContext;
public AllContactsAdapter(List<ContactVO> contactVOList, Context mContext){
this.contactVOList = contactVOList;
this.mContext = mContext;
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.single_contact_view, null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
ContactVO contactVO = contactVOList.get(position);
holder.tvContactName.setText(contactVO.getContactName());
holder.tvPhoneNumber.setText(contactVO.getContactNumber());
}
@Override
public int getItemCount() {
return contactVOList.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
ImageView ivContactImage;
TextView tvContactName;
TextView tvPhoneNumber;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
}
}
}
public类AllContactsAdapter扩展了RecyclerView.Adapter{
私人名单联系人;
私有上下文;
public AllContactsAdapter(列出contactVOList,上下文mContext){
this.contactVOList=contactVOList;
this.mContext=mContext;
}
@凌驾
公共联系人ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图视图=布局从(mContext)变平。充气(R.layout.single\u contact\u视图,空);
ContactViewHolder ContactViewHolder=新的ContactViewHolder(视图);
返回联系人视图持有者;
}
@凌驾
BindViewHolder上的公共无效(ContactViewHolder,内部位置){
ContactVO ContactVO=contactVOList.get(位置);
holder.tvContactName.setText(contactVO.getContactName());
holder.tvPhoneNumber.setText(contactVO.getContactNumber());
}
@凌驾
public int getItemCount(){
返回contactVOList.size();
}
公共静态类ContactViewHolder扩展了RecyclerView.ViewHolder{
图像视图;
TextView-tvContactName;
TextView-tvPhoneNumber;
公共联系人视图持有人(查看项目视图){
超级(项目视图);
ivContactImage=(ImageView)itemView.findViewById(R.id.ivContactImage);
tvContactName=(TextView)itemView.findViewById(R.id.tvContactName);
tvPhoneNumber=(TextView)itemView.findViewById(R.id.tvPhoneNumber);
}
}
}
这是我获取联系人的地方
private void getAllContacts() {
List<ContactVO> contactVOList = new ArrayList();
ContactVO contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactVO();
contactVO.setContactName(name);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setContactNumber(phoneNumber);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
contactVOList.add(contactVO);
}
}
AllContactsAdapter contactAdapter = new AllContactsAdapter(contactVOList, getApplicationContext());
rvContacts.setLayoutManager(new LinearLayoutManager(this));
rvContacts.setAdapter(contactAdapter);
}
private void getAllContacts(){
List contactVOList=new ArrayList();
ContactVO ContactVO;
ContentResolver ContentResolver=getContentResolver();
Cursor Cursor=contentResolver.query(ContactsContract.Contacts.CONTENT\u URI,null,null,null,ContactsContract.commondatatypes.Phone.DISPLAY\u NAME+“ASC”);
if(cursor.getCount()>0){
while(cursor.moveToNext()){
int hasPhoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
如果(hasPhoneNumber>0){
String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.\u id));
字符串名称=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_name));
contactVO=新contactVO();
contactVO.setContactName(名称);
Cursor phoneCursor=contentResolver.query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Phone.CONTACT_ID+“=?”,
新字符串[]{id},
无效);
if(phoneCursor.moveToNext()){
String phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
contactVO.setContactNumber(电话号码);
}
phoneCursor.close();
Cursor emailCursor=contentResolver.query(
ContactsContract.CommonDataTypes.Email.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Email.CONTACT_ID+“=?”,
新字符串[]{id},null);
while(emailCursor.moveToNext()){
String emailId=emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataTypes.Email.DATA));
}
contactVOList.add(contactVO);
}
}
AllContactsAdapter contactAdapter=新建AllContactsAdapter(contactVOList,getApplicationContext());
rvContacts.setLayoutManager(新的LinearLayoutManager(本));
rvContacts.setAdapter(contactAdapter);
}
此代码可能对您有所帮助
我检索所有联系人并将其绑定到Listview(您也可以在您的案例中使用RecyclerView)
接触活性
ContactListAdapter
行显示联系人。xml
您可以获得以下所有联系人:
获取联系人的代码在哪里?您刚刚显示了适配器代码。在后台获取联系人并在UI线程中更新UI。对不起,我missed@Piyush请立即检查它使用
AsyncTask
在doInBackGround()
方法中获取联系人,并在onPostExecute()中更新UI
)
method.Thank@Piyush将使用AsyncTask检查它
public class ContactsActivity extends AppCompatActivity {
String[] mProjection;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
mProjection = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Photo.PHOTO
};
cursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
mProjection,
null,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC"
);
ContactListAdapter adapter = new ContactListAdapter(getContext(),cursor);
final ListView listView = (ListView) findViewById(R.id.lvContactDisplay);
listView.setAdapter(adapter);
}
}
public class ContactListAdapter extends BaseAdapter {
private Context mContext;
private Cursor mCursor;
String[] mProjection;
// State of the row that needs to show separator
private static final int SECTIONED_STATE = 1;
// State of the row that need not show separator
private static final int REGULAR_STATE = 2;
// Cache row states based on positions
private int[] mRowStates;
public ContactListAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
mRowStates = new int[getCount()];
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
boolean showSeparator = false;
mProjection = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Photo.PHOTO
};
mCursor.moveToPosition(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_display_contacts, null);
}
else {
view = convertView;
}
ImageView profileUser = (ImageView) view.findViewById(R.id.ivProfileUser);
TextView contactNameView = (TextView) view.findViewById(R.id.tvUserContactName);
TextView contactId = (TextView) view.findViewById(R.id.tvUserContactId);
contactId.setVisibility(View.GONE);
String id = mCursor.getString(mCursor.getColumnIndex(mProjection[0]));
String name = mCursor.getString( mCursor.getColumnIndex(mProjection[1]));
int photo = mCursor.getInt(mCursor.getColumnIndex(mProjection[2]));
contactNameView.setText( name );
contactId.setText(id);
if (photo == 0){
profileUser.setImageResource(R.drawable.ic_profile_user);
}
else {
profileUser.setImageResource(photo);
}
switch (mRowStates[position]) {
case SECTIONED_STATE:
showSeparator = true;
break;
case REGULAR_STATE:
showSeparator = false;
break;
default:
if (position == 0) {
showSeparator = true;
}
else {
mCursor.moveToPosition(position - 1);
String previousName = mCursor.getString(mCursor.getColumnIndex(mProjection[1]));
char[] previousNameArray = previousName.toCharArray();
char[] nameArray = name.toCharArray();
if (nameArray[0] != previousNameArray[0]) {
showSeparator = true;
}
mCursor.moveToPosition(position);
}
// Cache it
mRowStates[position] = showSeparator ? SECTIONED_STATE : REGULAR_STATE;
break;
}
TextView separatorView = (TextView) view.findViewById(R.id.tvContactAlphabetOrder);
if (showSeparator) {
separatorView.setText(name.toCharArray(), 0, 1);
separatorView.setVisibility(View.VISIBLE);
}
else {
view.findViewById(R.id.tvContactAlphabetOrder).setVisibility(View.GONE);
}
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvContactAlphabetOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:padding="2dp"
android:background="#C8C8C8"
android:textSize="@dimen/font_size"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/ivProfileUser"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:src="@drawable/ic_profile_user"/>
<TextView
android:id="@+id/tvUserContactName"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="abc"
android:layout_gravity="center"
android:padding="2dp"
android:textColor="@color/navigationBarColor"
android:layout_marginLeft="5dp"
android:textSize="@dimen/font_size" />
<TextView
android:id="@+id/tvUserContactId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcdsfs"
android:layout_gravity="center"
android:padding="2dp"
android:textColor="@color/navigationBarColor"
android:layout_marginLeft="5dp"
android:textSize="@dimen/font_size" />
</LinearLayout>
</LinearLayout>