なぜ私のコードが開くのに時間がかかりそうですか?この断片を開くには、Android搭載デバイス(LG G4)で2〜4秒かかる。私はそれらの中で検索したいこれらの変数を持つ電話連絡先クラスを持っています。この連絡先ページをより速くするには?
public class PhoneContact {
public String contact_name = "";
public String contact_number = "";
public int contact_id = 0;
}
これらの連絡先を拡大するために、私はこれらのデータを表示するリストビューフラグメントを持っています。
カーソルが連絡先データベースを横断することができる場合チェックpublic void getPhoneContact() {
ArrayList<PhoneContact> phoneContacts = new ArrayList<PhoneContact>();
Cursor cursor = null;
ContentResolver contentResolver = getActivity().getContentResolver();
try{
cursor = contentResolver.query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
} catch (Exception e){
Log.e("Error on contact list", e.getMessage());
}
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
PhoneContact phoneContact = new PhoneContact();
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
phoneContact.contact_name = name;
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
, null
, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"
, new String[]{id}
, null);
while (phoneCursor.moveToNext()){
String number = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneContact.contact_number = number;
}
phoneCursor.close();
}
phoneContacts.add(phoneContact);
}
PhoneContactsAdapter adapter = new PhoneContactsAdapter(this.getContext(), phoneContacts);
ListView listView = (ListView) myFragmentView.findViewById(R.id.listview_phoneContacts);
listView.setAdapter(adapter);
}
}
:私はこれをしなかった私の携帯電話の連絡先情報を取得するために
最後public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState){
myFragmentView = inflater.inflate(R.layout.fragment_phonecontacts, container, false);
getPhoneContact();
return myFragmentView;
}
:私はこれを行うことによって、フラグメントを表示します。 それから各連絡先を通り、ID、名前、電話番号を取得します。 検索する連絡先がなくなると、カーソルを閉じて電話に連絡します。家の連絡先がアレイに追加され、アダプタを介してリストビューにバインドされます。
誰かがこれをスピードアップする方法や電話の連絡先を撤回する別の方法について提案はありますか?
アプリのオープニングに画面を表示し、recyclerviewを使用して複数の繰り返しを減らします(/ for)。 – jturolla
@jturolla連絡先からの反復を減らす方法に関する提案はありますか? – Shimy