Fix thread issue
This commit is contained in:
parent
af3d82ae8d
commit
f4da9df32f
1 changed files with 37 additions and 24 deletions
|
@ -57,6 +57,7 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
||||||
private int lastKnownPosition;
|
private int lastKnownPosition;
|
||||||
private Cursor cursor;
|
private Cursor cursor;
|
||||||
private List<Contact> contacts;
|
private List<Contact> contacts;
|
||||||
|
private Thread contactsHandler;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
@ -111,16 +112,17 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
cursor = Compatibility.getContactsCursor(getActivity().getContentResolver());
|
cursor = Compatibility.getContactsCursor(getActivity().getContentResolver());
|
||||||
contacts = new ArrayList<Contact>();
|
contactsHandler = new Thread(new Runnable() {
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
contacts = new ArrayList<Contact>();
|
||||||
for (int i = 0; i < cursor.getCount(); i++) {
|
for (int i = 0; i < cursor.getCount(); i++) {
|
||||||
Contact contact = getContact(i);
|
Contact contact = getContact(i);
|
||||||
contacts.add(contact);
|
contacts.add(contact);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
contactsHandler.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -138,28 +140,39 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
||||||
contactsList.setSelectionFromTop(lastKnownPosition, 0);
|
contactsList.setSelectionFromTop(lastKnownPosition, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
contactsHandler.interrupt();
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
private Contact getContact(int position) {
|
private Contact getContact(int position) {
|
||||||
cursor.moveToFirst();
|
try {
|
||||||
boolean success = cursor.move(position);
|
cursor.moveToFirst();
|
||||||
if (!success)
|
boolean success = cursor.move(position);
|
||||||
return null;
|
if (!success)
|
||||||
|
return null;
|
||||||
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
|
||||||
String name = Compatibility.getContactDisplayName(cursor);
|
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||||
Uri photo = Compatibility.getContactPictureUri(cursor, id);
|
String name = Compatibility.getContactDisplayName(cursor);
|
||||||
InputStream input = Compatibility.getContactPictureInputStream(getActivity().getContentResolver(), id);
|
Uri photo = Compatibility.getContactPictureUri(cursor, id);
|
||||||
|
InputStream input = Compatibility.getContactPictureInputStream(getActivity().getContentResolver(), id);
|
||||||
Contact contact;
|
|
||||||
if (input == null) {
|
Contact contact;
|
||||||
contact = new Contact(id, name);
|
if (input == null) {
|
||||||
}
|
contact = new Contact(id, name);
|
||||||
else {
|
}
|
||||||
contact = new Contact(id, name, photo, BitmapFactory.decodeStream(input));
|
else {
|
||||||
}
|
contact = new Contact(id, name, photo, BitmapFactory.decodeStream(input));
|
||||||
|
}
|
||||||
contact.setNumerosOrAddresses(Compatibility.extractContactNumbersAndAddresses(contact.getID(), getActivity().getContentResolver()));
|
|
||||||
|
contact.setNumerosOrAddresses(Compatibility.extractContactNumbersAndAddresses(contact.getID(), getActivity().getContentResolver()));
|
||||||
return contact;
|
|
||||||
|
return contact;
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ContactsListAdapter extends BaseAdapter implements SectionIndexer {
|
class ContactsListAdapter extends BaseAdapter implements SectionIndexer {
|
||||||
|
|
Loading…
Reference in a new issue