Fetching Contacts To A Recycler View Is Too Slow
I'm fetching contacts to recycler view... now the below codes just works great but it takes too much time on fetching contacts.... so if there is any way to get contacts fast than
Solution 1:
This code may help you
I retrieve all Contacts and Bind that contacts into Listview (You can use also RecyclerView in your case)
ContactsActivity
publicclassContactsActivityextendsAppCompatActivity {
String[] mProjection;
Cursor cursor;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
mProjection = newString[] {
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"
);
ContactListAdapteradapter=newContactListAdapter(getContext(),cursor);
finalListViewlistView= (ListView) findViewById(R.id.lvContactDisplay);
listView.setAdapter(adapter);
}
}
ContactListAdapter
publicclassContactListAdapterextendsBaseAdapter {
private Context mContext;
private Cursor mCursor;
String[] mProjection;
// State of the row that needs to show separatorprivatestaticfinalintSECTIONED_STATE=1;
// State of the row that need not show separatorprivatestaticfinalintREGULAR_STATE=2;
// Cache row states based on positionsprivateint[] mRowStates;
publicContactListAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
mRowStates = newint[getCount()];
}
@OverridepublicintgetCount() {
return mCursor.getCount();
}
@Overridepublic Object getItem(int position) {
returnnull;
}
@OverridepubliclonggetItemId(int position) {
return0;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
View view;
booleanshowSeparator=false;
mProjection = newString[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Photo.PHOTO
};
mCursor.moveToPosition(position);
if (convertView == null) {
LayoutInflaterinflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_display_contacts, null);
}
else {
view = convertView;
}
ImageViewprofileUser= (ImageView) view.findViewById(R.id.ivProfileUser);
TextViewcontactNameView= (TextView) view.findViewById(R.id.tvUserContactName);
TextViewcontactId= (TextView) view.findViewById(R.id.tvUserContactId);
contactId.setVisibility(View.GONE);
Stringid= mCursor.getString(mCursor.getColumnIndex(mProjection[0]));
Stringname= mCursor.getString( mCursor.getColumnIndex(mProjection[1]));
intphoto= 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);
StringpreviousName= 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;
}
TextViewseparatorView= (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;
}
}
row_display_contacts.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid: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"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:layout_gravity="center_horizontal"><ImageViewandroid:id="@+id/ivProfileUser"android:layout_width="50dp"android:layout_height="50dp"android:scaleType="fitXY"android:layout_gravity="center"android:src="@drawable/ic_profile_user"/><TextViewandroid: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" /><TextViewandroid: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>
You can get all Contacts like this:
Post a Comment for "Fetching Contacts To A Recycler View Is Too Slow"