3
\$\begingroup\$

I am working on an instant chat messaging application. I am getting contacts from phone and displayed them in list view of an activity, but it is taking 25-30 seconds using the following code:

1. On clicking Contact button:

contacts.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "Contacts Clicked", Toast.LENGTH_SHORT).show();
        popupWindow.dismiss();
        ArrayList<Bean_PhoneNumbers> listBeanPhone = getContactsFromPhone();
        Intent intent = new Intent(SingleChatActivity.this, PhoneBookActivity.class);
        //intent.putExtra("listBeanPhone", listBeanPhone);
        intent.putParcelableArrayListExtra("listBeanPhone", listBeanPhone);
        intent.putExtra("friendName", friendName);
        intent.putExtra("absoluteURL", completeURLFriend);
        intent.putExtra("friendID", frndID);
        startActivity(intent);
    }
});

2. getContactFromPhone() method

private ArrayList<Bean_PhoneNumbers> getContactsFromPhone() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            bean_phoneNumbers = new Bean_PhoneNumbers();
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(SingleChatActivity.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    bean_phoneNumbers.setName(name);
                    bean_phoneNumbers.setPhoneNumber(phoneNo);
                    listBeanPhoneNumbers.add(bean_phoneNumbers);
                }
                pCur.close();
            }

        }
    }
    return listBeanPhoneNumbers;
}

3.PhoneBookActivity.java

public class PhoneBookActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private ViewPager viewPager;
private SearchView searchView;
private ListView listView;
private Adapter_PhoneBook adapter_phoneBook;
private String friendName, completeURLFriend, frndID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phonebook);

    //findViewByID
    searchView = (SearchView) findViewById(R.id.search);
    listView = (ListView) findViewById(R.id.list);

    // Fetching data from a parcelable object passed from SingleChatActivity
    ArrayList<Bean_PhoneNumbers> listBeanPhone = getIntent().getParcelableArrayListExtra("listBeanPhone");
    for (int i = 0; i < listBeanPhone.size(); i++) {
        Bean_PhoneNumbers bean = listBeanPhone.get(i);
        String name = bean.getName();
        Log.e("Name", name);
    }
    //Initialization
    adapter_phoneBook = new Adapter_PhoneBook(PhoneBookActivity.this, listBeanPhone);
    listView.setAdapter(adapter_phoneBook);
    searchView.setOnQueryTextListener(this);

    //Getting values from previos screen(ChatFragment)
    Bundle bundle = getIntent().getExtras();
    friendName = bundle.getString("friendsName", null);
    completeURLFriend = bundle.getString("absoluteURL", null);
    frndID = bundle.getString("friendID");


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Bean_PhoneNumbers bean_phoneNumbers = (Bean_PhoneNumbers) adapterView.getItemAtPosition(i);
            Intent intent = new Intent(PhoneBookActivity.this, SingleChatActivity.class);
            intent.putExtra("bean", bean_phoneNumbers);
            intent.putExtra("friendName", friendName);
            intent.putExtra("absoluteURL", completeURLFriend);
            intent.putExtra("friendID", frndID);
            startActivity(intent);
        }
    });


}

@Override
public boolean onQueryTextSubmit(String s) {
    return false;
}

@Override
public boolean onQueryTextChange(String s) {
    adapter_phoneBook.getFilter().filter(s);
    return false;
}
}

Please review my code and tell me what I am doing wrong. I am getting contacts from phone and storing in an ArrayList. Then I am passing that arraylist to PhoneBookActivity which has a listview. I am populating the listview using a BaseAdapter.

\$\endgroup\$
  • \$\begingroup\$ Approved suggested code edit because it did look like a copy paste error; the OP seems to have tried making the code show up as a code block by hitting space. Their use of indentation is consistently good; whilst review of indentation might be on topic, I believe there is nothing to learn by the OP other than "how to use StackExchange Code Formatting". @DeepakR: Next time, paste the code into your question and use CTRL+K. \$\endgroup\$ – Pimgd Apr 7 '16 at 9:19
  • \$\begingroup\$ @Pimgd The space-edit was by me, OP used tabstopps if I recall right. Did I miss something? \$\endgroup\$ – J_F_B_M Apr 7 '16 at 14:36
  • 1
    \$\begingroup\$ @J_F_B_M codereview.stackexchange.com/review/suggested-edits/54855 there was a reject vote, whilst true in what it said ("Don'[t] edit code in questions on Code Review, even formatting may be discussed in a review"), it didn't apply in my opinion. But Improve & Edit overwrites the decision to accept a suggested edit regardless of other reviewers, which is why I felt the need to describe why it didn't apply here myself. \$\endgroup\$ – Pimgd Apr 7 '16 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.