/** * Create default data for application * * @param factor The multiplicative factor */ private void createDefaultData(int factor) { String[] names = getResources().getStringArray(R.array.list_name); TypedArray avatars = getResources().obtainTypedArray(R.array.list_avatar); String[] desList = getResources().getStringArray(R.array.list_description); int size = names.length; for (int i = 0; i < size; i++) { Bitmap avatar = BitmapFactory.decodeResource(getResources(), avatars.getResourceId(i, -1)); String name = names[i]; String des = desList[i]; User user = new User(factor * size + i, avatar, name, des, i); mUsers.add(user); mDbHelper.addUser(user); } }
/** Get first data for list contacts */ private void getDataForContacts(int start, int end) throws IndexOutOfBoundsException { // Get 8 first items in database mCurNumItems = start; List<User> userList = mDbHelper.getAllUsers(); TypedArray avatars = getResources().obtainTypedArray(R.array.list_avatar); for (int i = start; i < end; i++) { User user = userList.get(i); Bitmap avatar = BitmapFactory.decodeResource( getResources(), avatars.getResourceId(user.getIdAvatar(), -1)); user.setAvatar(avatar); mUsers.add(user); } mCurNumItems = end; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create database if it is not exist mDbHelper = new ContactDBHelper(getActivity()); mUsers = new ArrayList<User>(); if (mDbHelper.getAllUsers().size() == 0) { for (int i = 0; i < FACTOR_DATA; i++) { createDefaultData(i); } } mCurNumItems = 0; try { getDataForContacts(mCurNumItems, mCurNumItems + NUMBER_FIRST_ITEMS); } catch (IndexOutOfBoundsException ex) { ex.printStackTrace(); } mIsOverData = false; mAdapter = new ContactListAdapter(getActivity(), mUsers); }