Code example for Cursor

Methods: closegetCountmoveToPosition

0
	public Map<String, Contact> getContacts(int offset, int max_results, List<String> select, Map<String, Object> conditions) throws Exception {
		Map<String, Contact> contacts = new HashMap<String, Contact>();
		 
		Cursor cursor = cr.query(People.CONTENT_URI, null, null, null, null);
		try { 
			if (!cursor.moveToPosition(offset))
				return contacts;
			if (max_results == -1) {
				max_results = cursor.getCount() - offset;
			} 
			 
			do { 
				if(contacts.size() >= max_results) {
					break; 
				} 
				Contact contact = new Contact();
	 
				String id = cursor.getString(cursor.getColumnIndex(People._ID));
				contact.setId(id);
	 
				//contact.setField(PB_COMPANY_NAME, cursor.getString(cursor.getColumnIndex(People.COMPANY))); 
	 
				//contact.setField(Phonebook.PB_FIRST_NAME, ""); 
				//contact.setField(Phonebook.PB_LAST_NAME, ""); 
				String name = cursor.getString(cursor.getColumnIndex(People.NAME));
				if (name != null) {
				    contact.setField(Phonebook.PB_DISPLAY_NAME, name);
					String[] names = name.split(" ");
	 
					if (names.length == 1) {
						contact.setField(Phonebook.PB_FIRST_NAME, names[0]);
					} 
					else if (names.length > 1) {
						contact.setField(Phonebook.PB_FIRST_NAME, names[0]);
						contact.setField(Phonebook.PB_LAST_NAME, name.replaceFirst(names[0] + " ", ""));
					} 
				} 
	 
				fillPhones(id, contact);
	 
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(contact.id()));
	 
				Uri orgUri = Uri.withAppendedPath(uri,
						Contacts.Organizations.CONTENT_DIRECTORY);
	 
				String[] organizationProjection = new String[] { Organizations.COMPANY };
				Cursor organizationCursor = cr.query(orgUri,
						organizationProjection, "person=?", new String[] {id}, null);
				try { 
					int organizationCursorCount = organizationCursor.getCount();
					if (organizationCursorCount > 0) {
						organizationCursor.moveToFirst();
						int numberColumn = organizationCursor
								.getColumnIndex(Organizations.COMPANY);
		 
						if (numberColumn != -1)
							contact.setField(Phonebook.PB_COMPANY_NAME,
									organizationCursor.getString(numberColumn));
					} 
				} 
				finally { 
					organizationCursor.close();
				} 
	 
				String[] contactProjection = new String[] {
						Contacts.ContactMethods.KIND,
						Contacts.ContactMethods.DATA };
	 
				Cursor contactCursor = cr.query(
						Contacts.ContactMethods.CONTENT_URI,
						contactProjection, "person=?", new String[] {id}, null);
				try { 
					int contactCursorCount = contactCursor.getCount();
					if (contactCursorCount > 0) {
						contactCursor.moveToFirst();
						int numberColumn = contactCursor
								.getColumnIndex(Contacts.ContactMethods.DATA);
						int typeColumn = contactCursor
								.getColumnIndex(Contacts.ContactMethods.KIND);
						for (int j = 0; j < contactCursorCount; j++) {
							switch (contactCursor.getInt(typeColumn)) {
							case Contacts.ContactMethods.TYPE_HOME:
								contact.setField(Phonebook.PB_EMAIL_ADDRESS, contactCursor.getString(numberColumn));
								break; 
							} 
							contactCursor.moveToNext();
						} 
					} 
				} 
				finally { 
					contactCursor.close();
				} 
//				contact.makeAllFilled(); 
				contacts.put(contact.getField(Phonebook.PB_ID), contact);
			} while (cursor.moveToNext());
		} 
		finally { 
			cursor.close();
		} 
		 
		return contacts;
	} 
 
Stop searching for code, let great code find you!  Add Codota to your java IDE