2017-07-01 54 views
1

您好,我需要按照字母順序排序我的應用程序中的聯繫人列表。應用程序檢查哪個設備聯繫人在應用程序中註冊並將其放置在應用程序聯繫人列表中我有自定義簡單的遊標適配器:按字母順序排列聯繫人列表清單

package com.loopg.adapters; 

       import android.content.Context; 
       import android.database.Cursor; 
       import android.support.v4.widget.SimpleCursorAdapter; 
       import android.view.LayoutInflater; 
       import android.view.View; 
       import android.view.ViewGroup; 
       import android.widget.AlphabetIndexer; 
       import android.widget.CheckBox; 
       import android.widget.SectionIndexer; 
       import android.widget.TextView; 

       import com.loopg.R; 
       import com.loopg.db.AppContacts; 
       import com.loopg.db.AppContactsData; 
       import com.loopg.db.AppProfiles; 

       import java.util.HashMap; 
       import java.util.Iterator; 
       import java.util.Map; 
       import java.util.Set; 
       import java.util.TreeMap; 

       public class AllContactsAdapter extends SimpleCursorAdapter implements SectionIndexer { 

        private static final int TYPE_HEADER = 1; 
        private static final int TYPE_NORMAL = 0; 

        private static final int TYPE_COUNT = 2; 
        public HashMap<String, Boolean> hmSelected = new HashMap<String, Boolean>(); 
        public int selectedContacts = 0; 
        Cursor mCursor = null; 
        private AlphabetIndexer indexer; 
        private int[] usedSectionNumbers; 
        private Map<Integer, Integer> sectionToOffset; 
        private Map<Integer, Integer> sectionToPosition; 
        private boolean showSelection = false; 
        private int rowCount = 0; 
        private int selectedNumbers = 0; 
        private Context invokerContext = null; 
        private String appStatus = ""; 
        private String inviteStatus = ""; 

        private String sectionHeaders = "#GHGJHJHKHJKGJHKLKJHGF"; 

        public AllContactsAdapter(Context context, Cursor c, int layout, boolean forInvite) { 
         super(context, layout, c, new String[]{}, new int[]{}, 0); 
         invokerContext = context; 
         showSelection = forInvite; 
         mCursor = c; 

         initialize(); 

         appStatus = context.getString(R.string.tw_default_status); 
         inviteStatus = context.getString(R.string.hf_invite_status); 

        } 

        public AllContactsAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { 
         super(context, layout, c, from, to, flags); 
        } 

        @Override 
        public void changeCursor(Cursor cursor) { 
         super.changeCursor(cursor); 

         mCursor = cursor; 

         initialize(); 
        } 

        private void initialize() { 

         hmSelected = new HashMap<String, Boolean>(); 

         rowCount = mCursor.getCount(); 

         indexer = new AlphabetIndexer(mCursor, mCursor.getColumnIndex(AppContacts.CLS.CONTACT_NAME), sectionHeaders); 
         // use a TreeMap because we are going to iterate over its keys in sorted 
         // order 
         sectionToPosition = new TreeMap<Integer, Integer>(); 
         sectionToOffset = new HashMap<Integer, Integer>(); 

         final int count = super.getCount(); 

         int i; 
         // temporarily have a map alphabet section to first index it appears 
         // (this map is going to be doing something else later) 
         for (i = count - 1; i >= 0; i--) { 
          sectionToPosition.put(indexer.getSectionForPosition(i), i); 
         } 

         i = 0; 
         usedSectionNumbers = new int[sectionToPosition.keySet().size()]; 

         // note that for each section that appears before a position, we must 
         // offset our 
         // indices by 1, to make room for an alphabetical header in our list 
         for (Integer section : sectionToPosition.keySet()) { 
          sectionToOffset.put(section, i); 
          usedSectionNumbers[i] = section; 
          i++; 
         } 

         // use offset to map the alphabet sections to their actual indicies in 
         // the list 
         for (Integer section : sectionToPosition.keySet()) { 
          sectionToPosition.put(section, sectionToPosition.get(section) + sectionToOffset.get(section)); 
         } 

         if (showSelection) { 
          initializeSelection(); 
         } 
         selectedContacts = 0; 
        } 

        private void initializeSelection() { 

         int position = mCursor.getPosition(); 

         if (mCursor.moveToFirst()) { 
          do { 
           String id = mCursor.getString(mCursor.getColumnIndex("_id")); 

           hmSelected.put(id, false); 
          } while (mCursor.moveToNext()); 
         } 

         mCursor.moveToPosition(position); 
        } 

        @Override 
        public void bindView(View view, Context context, Cursor cursor) { 
         super.bindView(view, context, cursor); 

         String name = cursor.getString(cursor.getColumnIndex(AppContacts.CLS.CONTACT_NAME)); 
         String status = cursor.getString(cursor.getColumnIndex(AppProfiles.CLS.STATUS)); 
         if (status == null || status.length() == 0) { 
          int appUser = cursor.getInt(cursor.getColumnIndex(AppContactsData.CLS.CONTACT_APP_USER)); 
          if (appUser == AppContacts.APP_USER) 
           status = appStatus; 
          else 
           status = inviteStatus; 
         } 

         String id = cursor.getString(cursor.getColumnIndex("_id")); 

         view.setTag(id); 

         TextView tv = (TextView) view.findViewById(R.id.id_tv_ContactName); 
         tv.setText(name); 
         tv = (TextView) view.findViewById(R.id.id_tv_ContactStatus); 
         tv.setText(status); 

         CheckBox cb = (CheckBox) view.findViewById(R.id.id_cb_ContactSelected); 
         if (showSelection) { 
          // registerForClickListner(cb); 
          if (cb.getVisibility() != View.VISIBLE) 
           cb.setVisibility(View.VISIBLE); 

          boolean checked = hmSelected.get(id) == null ? false : hmSelected.get(id); 
          if (checked) { 
           cb.setChecked(true); 
          } else { 
           cb.setChecked(false); 
          } 
         } else { 
          if (cb.getVisibility() != View.GONE) 
           cb.setVisibility(View.GONE); 
         } 
        } 

        @Override 
        public View newView(Context context, Cursor cursor, ViewGroup parent) { 
         LayoutInflater inflator = LayoutInflater.from(context); 

         View view = inflator.inflate(R.layout.view_contact_row, null); 

         return view; 
        } 

        @Override 
        public int getCount() { 
         if (super.getCount() != 0) { 
          return super.getCount() + usedSectionNumbers.length; 
         } 

         return 0; 
        } 

        @Override 
        public Object getItem(int position) { 
         if (getItemViewType(position) == TYPE_NORMAL) {// we define this 
          return super.getItem(position - sectionToOffset.get(getSectionForPosition(position)) - 1); 
         } 

         return null; 
        } 

        @Override 
        public int getPositionForSection(int section) { 
         if (!sectionToOffset.containsKey(section)) { 
          int i = 0; 
          int maxLength = usedSectionNumbers.length; 
          while (i < maxLength && section > usedSectionNumbers[i]) { 
           i++; 
          } 
          if (i == maxLength) 
           return getCount(); // the given section is past all our data 

          return indexer.getPositionForSection(usedSectionNumbers[i]) + sectionToOffset.get(usedSectionNumbers[i]); 
         } 

         return indexer.getPositionForSection(section) + sectionToOffset.get(section); 
        } 

        @Override 
        public int getSectionForPosition(int position) { 
         int i = 0; 
         int maxLength = usedSectionNumbers.length; 

         // linear scan over the used alphabetical sections' positions 
         // to find where the given section fits in 
         while (i < maxLength && position >= sectionToPosition.get(usedSectionNumbers[i])) { 
          i++; 
         } 
         return usedSectionNumbers[i - 1]; 
        } 

        @Override 
        public Object[] getSections() { 
         return indexer.getSections(); 
        } 

        // nothing much to this: headers have positions that the sectionIndexer 
        // manages. 
        @Override 
        public int getItemViewType(int position) { 
         if (position == getPositionForSection(getSectionForPosition(position))) { 
          return TYPE_HEADER; 
         } 
         return TYPE_NORMAL; 
        } 

        @Override 
        public int getViewTypeCount() { 
         return TYPE_COUNT; 
        } 

        // return the header view, if it's in a section header position 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
         final int type = getItemViewType(position); 
         if (type == TYPE_HEADER) { 
          if (convertView == null) { 
           convertView = LayoutInflater.from(invokerContext).inflate(R.layout.view_list_header, parent, false); 
          } 
          ((TextView) convertView.findViewById(R.id.id_tv_ListHeader)).setText((String) getSections()[getSectionForPosition(position)]); 
          return convertView; 
         } 
         return super.getView(position - sectionToOffset.get(getSectionForPosition(position)) - 1, convertView, parent); 
        } 


        // these two methods just disable the headers 
        @Override 
        public boolean areAllItemsEnabled() { 
         return false; 
        } 

        @Override 
        public boolean isEnabled(int position) { 
         if (getItemViewType(position) == TYPE_HEADER) { 
          return false; 
         } 
         return true; 
        } 



        public void selectAll() { 
         Set<String> set = hmSelected.keySet(); 
         Iterator<String> iterator = set.iterator(); 

         while (iterator.hasNext()) { 
          String id = iterator.next(); 

          hmSelected.put(id, true); 
         } 
        } 
       } 

這裏是我設置適配器:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.frag_all_contacts, null); 

    String query = "SELECT * FROM " + AppContactsData.TABLE_CONTACT_DATA + " LEFT JOIN " + AppContacts.TABLE_CONTACT_NAMES + " ON " 
      + AppContactsData.CLS.CONTACT_ID + "=" + AppContacts.CLS.CONTACT_ID + " LEFT JOIN " + AppProfiles.TABLE_PROFILES + " ON " 
      + AppProfiles.CLS.USER_NUMBER + "=" + AppContactsData.CLS.CONTACT_E164NUMBER + " where " + AppContactsData.CLS.CONTACT_ID 
      + "!=0"; 

    query = query + " ORDER BY " + AppContacts.CLS.CONTACT_NAME + " collate nocase"; 

    cursor = DBHelper.getInstance().executeRawQuery(query); 

    ListView lv = (ListView) view.findViewById(R.id.id_lv_AllContacts); 

    adapter = new AllContactsAdapter(getActivity(), cursor, R.layout.view_contact, showSelection); 
    listview.setAdapter(adapter); 
+0

現在發生了什麼?查詢似乎很好,你是否按照alfabetical的順序檢索數據? – ImAtWar

+0

聯繫人不按字母順序顯示。 –

回答

0

可能它會幫助!

改變線

查詢=查詢+ 「ORDER BY」 + AppContacts.CLS.CONTACT_NAME + 「整理NOCASE」;

喜歡這個

查詢=查詢+ 「ORDER BY」 + AppContacts.CLS.CONTACT_NAME + 「整理NOCASE ASC」;