package com.thinkit.lewebconnect; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.SectionIndexer; import android.widget.TextView; public class LeWebAdapter extends ArrayAdapter { private final Context context; public ArrayList users; /** * Lock used to modify the content of {@link #mObjects}. Any write operation * performed on the array should be synchronized on this lock. This lock is also * used by the filter (see {@link #getFilter()} to make a synchronized copy of * the original array of data. */ private final Object mLock = new Object(); private ArrayList originalUsers; private LeWebFilter filter; private boolean mNotifyOnChange = true; private HashMap alphaIndexer; private String[] sections = new String[0]; private boolean enableSections; private boolean companySections; private boolean countrySections; public LeWebAdapter(Context context, ArrayList users, boolean enableSections, boolean companySections, boolean countrySections) { super(context, R.layout.rowlayout, users); // Log.d("LeWebAdapter", "LeWebAdapter constructor"); this.context = context; this.users = users; this.filter = new LeWebFilter(); this.enableSections = enableSections; this.companySections = companySections; this.countrySections = countrySections; if(enableSections) { // Log.d("LeWebAdapter", "Sections enabled"); // alphaIndexer = new HashMap(); // String firstChar; // for(int i = users.size() - 1; i >= 0; i--) // { // Attendee element = users.get(i); // if (countrySections) // firstChar = element.getCountry().substring(0, 1).toUpperCase(); // else if (companySections) // { // Log.d("Sections", "Enabling company section, getting first letter"); // firstChar = element.getCompany().substring(0, 1).toUpperCase(); // } // else // firstChar = element.getLname().substring(0, 1).toUpperCase(); // // if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A') // firstChar = "A"; // // alphaIndexer.put(firstChar, i); // } // // Set keys = alphaIndexer.keySet(); //// Log.d("LeWebAdapter", keys.toString()); // Iterator it = keys.iterator(); // ArrayList keyList = new ArrayList(); // // while(it.hasNext()) // keyList.add(it.next()); // // Collections.sort(keyList); // sections = new String[keyList.size()]; // keyList.toArray(sections); //// Log.d("LeWebAdapter", keyList.toString()); //// Log.d("LeWebAdapter", alphaIndexer.toString()); buildHashIndexer(); buildSections(); } } public void buildSections() { Set keys = alphaIndexer.keySet(); Iterator it = keys.iterator(); ArrayList keyList = new ArrayList(); while(it.hasNext()) keyList.add(it.next()); Collections.sort(keyList); sections = new String[keyList.size()]; keyList.toArray(sections); } public void buildHashIndexer() { try { // Log.d("LeWebAdapter", "Building hashindexer"); alphaIndexer = new HashMap(); String firstChar; for(int i = users.size() - 1; i >= 0; i--) { Attendee element = users.get(i); if (countrySections) firstChar = element.getCountry().substring(0, 1).toUpperCase(); else if (companySections) { Log.d("Sections", "Enabling company section, getting first letter"); firstChar = element.getCompany().substring(0, 1).toUpperCase(); } else firstChar = element.getLname().substring(0, 1).toUpperCase(); if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A') firstChar = "A"; alphaIndexer.put(firstChar, i); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void notifyDataSetChanged(){ if (enableSections) { // Log.d("notifyDataSetChanged", "..."); buildHashIndexer(); buildSections(); } super.notifyDataSetChanged(); mNotifyOnChange = true; } @Override public void notifyDataSetInvalidated() { if(enableSections) { // Log.d("notifyDataSetInvalidated", "..."); buildHashIndexer(); buildSections(); super.notifyDataSetInvalidated(); } } public void setNotifyOnChange(boolean notifyOnChange){ mNotifyOnChange = notifyOnChange; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowview = inflater.inflate(R.layout.rowlayout, parent, false); //Log.d("getView", "Making row ...."); try { Attendee user = users.get(position); if (user != null) { TextView name_text = (TextView) rowview.findViewById(R.id.name_text); TextView company = (TextView) rowview.findViewById(R.id.company_text); TextView country = (TextView) rowview.findViewById(R.id.country_text); TextView likes = (TextView) rowview.findViewById(R.id.likes); if (name_text != null) name_text.setText(user.getLname() + " " + user.getFname()); if (company != null) company.setText(user.getCompany()); if (country != null) country.setText(user.getCountry()); if (likes != null) likes.setText(String.valueOf(user.getLikes())); setSocial(rowview, user); if(enableSections && getSectionForPosition(position) != getSectionForPosition(position - 1)) { // Log.d("getView", "Setting Header ........ " + String.valueOf(position)); TextView h = (TextView) rowview.findViewById(R.id.header); h.setText(sections[getSectionForPosition(position)]); h.setVisibility(View.VISIBLE); } else { TextView h = (TextView) rowview.findViewById(R.id.header); h.setVisibility(View.GONE); } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return rowview; } private void setSocial(View rowview, Attendee user) { // TODO Auto-generated method stub boolean twitter; boolean linkedin; boolean facebook; ImageView imageView = (ImageView) rowview.findViewById(R.id.icons); facebook = user.isHas_facebook(); linkedin = user.isHas_linkedin(); twitter = user.isHas_twitter(); if (facebook && twitter && linkedin){ imageView.setImageResource(R.drawable.social_all); } else if (facebook && twitter) { imageView.setImageResource(R.drawable.no_linkedin); } else if (facebook && linkedin) { imageView.setImageResource(R.drawable.no_twitter); } else if (twitter && linkedin) { imageView.setImageResource(R.drawable.no_fb); } else if (twitter) { imageView.setImageResource(R.drawable.twitter); } else if (facebook) { imageView.setImageResource(R.drawable.fb); } else if (linkedin) { imageView.setImageResource(R.drawable.linkedin); } else imageView.setImageResource(R.drawable.no_all); } @Override public int getCount(){ return this.users.size(); } public int getPositionForSection(int section) { try { if(!enableSections) return 0; if (section == sections.length) section--; String letter = sections[section]; return alphaIndexer.get(letter); } catch (Exception e) { // TODO: handle exception // Log.d("getPositionForSection", String.valueOf(section)); // Log.d("getPositionForSection", "alphaindexer: " + alphaIndexer.toString()); // Log.d("getPositionForSection", "sections: " + getSections()); e.printStackTrace(); } return 0; } public int getSectionForPosition(int position) { // Log.d("getSectionForPosition", "recievied position : " + String.valueOf(position)); if(!enableSections) return 0; int prevIndex = 0; // for(int i = 0; i < sections.length; i++) // { // if(getPositionForSection(i) > position && prevIndex <= position) // { // prevIndex = i ; // break; // } // prevIndex = i; // } int i = 0; while (i < sections.length) { // Log.d("i", "------------>" + String.valueOf(i)); // Log.d("sectionsLength", "------------>" + String.valueOf(sections.length)); if (getPositionForSection(i) > position && prevIndex <= position) { prevIndex = i; break; } prevIndex = i; i++; } prevIndex--; // Log.d("getSectionForPosition", "Returning : " + String.valueOf(prevIndex)); return prevIndex; } public Object[] getSections() { return sections; } @Override public android.widget.Filter getFilter() { if(filter == null) filter = new LeWebFilter(); return filter; } // Currently filters only by Lname private class LeWebFilter extends android.widget.Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { // NOTE: this function is *always* called from a background thread, and // not the UI thread. // Log.d("Filter", "Filter ....."); FilterResults results = new FilterResults(); if (originalUsers == null) { synchronized (mLock) { originalUsers = new ArrayList(users); } } if (constraint == null || constraint.length() == 0) { synchronized (mLock) { ArrayList list = new ArrayList(originalUsers); results.values = list; results.count = list.size(); } } else { try { String constraintStr = constraint.toString().toLowerCase(); final ArrayList values = originalUsers; final int count = values.size(); final ArrayList newValues = new ArrayList(); for (int i = 0; i < count; i++) { final Attendee value = values.get(i); final String valueText; if (companySections) { valueText = value.getCompany().toLowerCase(); } else if (countrySections){ valueText = value.getCountry().toLowerCase(); } else { valueText = value.getLname().toLowerCase() + " " + value.getFname().toLowerCase(); } // First match against the whole, non-splitted value if (valueText.startsWith(constraintStr)) { newValues.add(value); } else { final String[] words = valueText.split(" "); final int wordCount = words.length; for (int k = 0; k < wordCount; k++) { if (words[k].startsWith(constraintStr)) { newValues.add(value); break; } } } } results.values = newValues; results.count = newValues.size(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return results; } // constraint = constraint.toString().toLowerCase(); // if(constraint != null && constraint.toString().length() > 0) // { // Log.d("Filter", "constraint not null with value <" + constraint.toString() + ">"); // ArrayList filt = new ArrayList(); // ArrayList lItems = new ArrayList(); // synchronized (this) // { // lItems.addAll(users); // } // for(int i = 0, l = lItems.size(); i < l; i++) // { // Attendee m = lItems.get(i); // if (countrySections && m.getCountry().toLowerCase().contains(constraint)) // filt.add(m); // else if (companySections && m.getCompany().toLowerCase().contains(constraint)) // filt.add(m); // else if (m.getLname().toLowerCase().contains(constraint)) // filt.add(m); // } // result.count = filt.size(); // result.values = filt; // } //// else if (constraint == null || constraint.length() == 0) //// { //// ArrayList list = new ArrayList(originalUsers); //// result.values=list; //// result.count = list.size(); //// } // else // { // synchronized(this) // { // result.values = users; // result.count = users.size(); // } // } // return result; @SuppressWarnings("unchecked") protected void publishResults(CharSequence constraint, FilterResults results) { // NOTE: this function is *always* called from the UI thread. users = (ArrayList)results.values; if (results.count > 0) { // buildHashIndexer(); // buildSections(); notifyDataSetChanged(); } else { // buildHashIndexer(); // buildSections(); notifyDataSetInvalidated(); } // notifyDataSetChanged(); // clear(); // for(int i = 0, l = originalUsers.size(); i < l; i++) // add(originalUsers.get(i)); // notifyDataSetInvalidated(); } } }