public void setAdapter(final ArrayAdapter<?> adapter) { if (mAdapter != null) { mAdapter.unregisterDataSetObserver(mOb); } mAdapter = adapter; adapter.registerDataSetObserver(mOb); loadData(); }
public void init() { db.open(); usersArray = db.getAllUsers(); db.close(); // usersArray.add(new UserInstance(1, "Cauchy", "Choi", "*****@*****.**", // "4084991666", 2, 3)); // usersArray.add(new UserInstance(2, "Tina", "Chen", "*****@*****.**", "6263761106", 2, 3)); // Gets the contact list and saves it into contactList ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor.getCount() > 0) { while (cursor.moveToNext()) { // Get name from contact list String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // currentSearchList.add(name); contactList.add(name); // Log.d("Contact List", name); // Get phone number from contact list String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (hasPhone.equalsIgnoreCase("1")) hasPhone = "true"; else hasPhone = "false"; if (Boolean.parseBoolean(hasPhone)) { Cursor phones = getContentResolver() .query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); while (phones.moveToNext()) { String phoneNumber = phones .getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) .replaceAll("\\D", ""); // Log.d("TEST", phoneNumber); for (int i = 0; i < usersArray.size(); i++) { // Log.d("TEST1", // phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); if (usersArray.get(i).getUserPhone().equals(phoneNumber)) { currentSearchList.add( phones.getString( phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); fullSearchList.add(usersArray.get(i)); currSearchList.add(usersArray.get(i)); } } } phones.close(); } } } // Sets up the adapter for AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_names); adapter = new ArrayAdapter<String>(this, R.layout.search, currentSearchList); textView.setAdapter(adapter); textView.setThreshold(1); textView.setDropDownHeight(0); // Sets the current contact list mContactList = (GridView) findViewById(R.id.gridview); uAdapter = new SearchAdapter(this, fullSearchList, checkedUsers); mContactList.setAdapter(uAdapter); mContactList.requestFocus(); mContactList.setOnItemClickListener(this); /*mContactList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Log.d("itemClick", "item clicked"); if (!checkedUsers.contains(parent.getItemAtPosition(position))) { checkedUsers.add((UserInstance)parent.getItemAtPosition(position)); } else { checkedUsers.remove((UserInstance)parent.getItemAtPosition(position)); } for (int i = 0; i < checkedUsers.size(); i++) { Log.d("checkedUsers", checkedUsers.get(i).getUserFirstName() + " " + checkedUsers.get(i).getUserLastName()); } Toast.makeText(Search.this, "" + position, Toast.LENGTH_SHORT).show(); } });*/ adapter.registerDataSetObserver( new DataSetObserver() { @Override public void onChanged() { currSearchList.clear(); currentSearchList.clear(); for (int i = 0; i < fullSearchList.size(); i++) { if (textView .getText() .toString() .toLowerCase() .equals( fullSearchList .get(i) .getUserFirstName() .substring(0, textView.getText().toString().length()) .toLowerCase()) || textView .getText() .toString() .toLowerCase() .equals( fullSearchList .get(i) .getUserLastName() .substring(0, textView.getText().toString().length()) .toLowerCase())) { currSearchList.add(fullSearchList.get(i)); Log.d( "TEST", fullSearchList.get(i).getUserFirstName() + " " + fullSearchList.get(i).getUserLastName()); } } for (int i = 0; i < adapter.getCount(); i++) { Object item = adapter.getItem(i); currentSearchList.add(item.toString()); } uAdapter = new SearchAdapter(Search.this, currSearchList, checkedUsers); mContactList.setAdapter(uAdapter); } @Override public void onInvalidated() { Log.d("OnInvalidated", "invalid data"); currentSearchList.clear(); currSearchList.clear(); uAdapter = new SearchAdapter(Search.this, currSearchList, checkedUsers); mContactList.setAdapter(uAdapter); } }); }
@Override public void registerDataSetObserver(DataSetObserver paramDataSetObserver) { currentDataSetObserver = paramDataSetObserver; spinnerAdapter.registerDataSetObserver(paramDataSetObserver); }