私は、電話連絡先リストから複数の連絡先を読む必要があるアプリを開発しています。私は2000人のメンバーが私の電話帳に追加し、私はすべての連絡先を正常に取得したとします。しかし問題は、私が2000年の連絡先のレイアウトを描いているとき、私のデバイスは全員が画面に描かなくなるまでフリーズするということです。連絡先リストから2000人のメンバーを凍らせる私のアプリをフリーズ
電話帳から連絡先をすべて取得するには、contentResolverを使用します。
public void getAllContacts(ContentResolver cr)
{
try {
final String[] PROJECTION = new String[]
{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
name1.clear();
phno1.clear();
MatrixCursor mc = new MatrixCursor(new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
});
String lastNumber = "";
Cursor c1 = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
while(c1.moveToNext())
{
try {
String name = c1.getString(c1.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = c1.getString(c1.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String test = number.replaceAll(" ", "");
if (!lastNumber.contains(test)) {
lastNumber = test;
mc.addRow(new String[]{name, test});
}
}catch (Exception e)
{
e.printStackTrace();
}
}
c1.close();
while (mc.moveToNext())
{
String value = "";
String name =mc.getString(mc.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = mc.getString(mc.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String result = name.replaceAll("[!.#^$%&()[email protected]_|?*<\":>+\\[\\]/']", " ");
String test = phoneNumber.replaceAll("[!.#^$%&()[email protected]_|?*<\":>+\\[\\]/']", "");
String numbers = test.substring(Math.max(0, test.length() - 10));
name1.add(result);
phno1.add(numbers);
}
mc.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
Drawing contact layout from added contact to Arraylist.
public void Drawcontactlayout()
{
try {
contactlist.removeAllViews(); // Linear Layout
for (int i = 0; i < name1.size(); i++)
{
try {
int endIndex = searchtext.length();
String nm = name1.get(i).toString();
String test1 = phno1.get(i).toString();
String s = searchtext.substring(0, endIndex);
String p = test1.substring(0, endIndex);
String n = nm.substring(0, endIndex);
String searchv = s.toUpperCase();
if (n.equalsIgnoreCase(s) || p.equalsIgnoreCase(s) || nm.contains(s) || nm.contains(searchv) || test1.contains(s) || searchtext.equalsIgnoreCase("")) {
rl1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout layout = new RelativeLayout(ct);
layout.setLayoutParams(rl1);
final int finalI = i;
rl1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView iname = new TextView(ct);
iname.setPadding(width/70, 0, 0, 0);
iname.setId(i + 1);
iname.setText(name1.get(i).toString());
iname.setGravity(Gravity.CENTER);
iname.setTextSize(12);
iname.setTextColor(getResources().getColor(R.color.white_light));
iname.setLayoutParams(rl1);
rl1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rl1.addRule(RelativeLayout.BELOW, iname.getId());
rl1.setMargins(width/70, width/70, 0, 0);
TextView mobile = new TextView(ct);
mobile.setId(i + 2);
mobile.setTextSize(12);
mobile.setText(phno1.get(i).toString());
mobile.setTextColor(getResources().getColor(R.color.white_light));
mobile.setLayoutParams(rl1);
rl1 = new RelativeLayout.LayoutParams(width/12, width/12);
rl1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rl1.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
rl1.setMargins(0, 0, width/50, 0);
final TextView check = new TextView(ct);
check.setId(i + 3);
try {
if (checked) {
check.setBackgroundResource(android.R.drawable.checkbox_on_background);
names.add(name1.get(finalI).toString());
numbers.add(phno1.get(finalI).toString());
} else {
if (testing[finalI] != false) {
check.setBackgroundResource(android.R.drawable.checkbox_on_background);
} else {
names.clear();
numbers.clear();
check.setBackgroundResource(android.R.drawable.checkbox_off_background);
}
}
check.setLayoutParams(rl1);
checkbox.add(check);
}catch (Exception e)
{
e.printStackTrace();
}
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (CommanString.REMAINING == 0) {
Toast.makeText(ct, "Insuficient Slot Invitees, please purchase more!", Toast.LENGTH_SHORT).show();
} else {
if (testing[finalI] == false) {
testing[finalI] = true;
names.add(name1.get(finalI).toString());
numbers.add(phno1.get(finalI).toString());
check.setBackgroundResource(android.R.drawable.checkbox_on_background);
} else if (testing[finalI] == true) {
testing[finalI] = false;
Drawable img4 = ct.getResources().getDrawable(android.R.drawable.checkbox_off_background);
img4.setBounds(0, 0, width/12, width/12);
chkall.setCompoundDrawables(null, null, img4, null);
chkall.setCompoundDrawablePadding(width/50);
isChecked = false;
checked = false;
for (int i = 0; i < names.size(); i++) {
if (names.get(i).toString().equalsIgnoreCase(name1.get(finalI).toString()) || numbers.get(finalI).toString().equalsIgnoreCase(phno1.get(finalI).toString())) {
// CommanString.REMAINING = CommanString.REMAINING +1;
names.remove(i);
numbers.remove(i);
}
}
check.setBackgroundResource(android.R.drawable.checkbox_off_background);
}
}
}catch (Exception e)
{
e.printStackTrace();
}
}
});
rl1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, width/300);
rl1.setMargins(0, width/50, 0, 0);
TextView margin = new TextView(ct);
margin.setBackgroundColor(Color.parseColor("#cccccc"));
margin.setLayoutParams(rl1);
layout.addView(iname);
layout.addView(mobile);
layout.addView(check);
contactlist.addView(layout);
contactlist.addView(margin);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}