mercredi 1 juillet 2015

Can I use max(address) as order by parameter in Android sqlite?

I hope to get a address list and corresponding Thread_id list, so I write the function GetListPhoneNumberForConversation and GetListThread_idForConversation.

You know sometimes the address will retun null in draft sms. Please view the document getting "null" as an address for a draft sms

So, I use max(address) as order by parameter, I hope that it can exclude null address record.

It can do return not null address, but I don't know if my code is correct and the address list and the Thread_id list are matched.

   private static List<String> GetListPhoneNumberForConversation(Context myContext){
        return GetListByField(myContext,"address");
   }

   public static List<String> GetListThread_idForConversation(Context myContext){
        return GetListByField(myContext,"thread_id");
   }

   private  static  List<String> GetListByField(Context myContext,String field){
        List<String> listbyField=new ArrayList<String>();

        Uri uri=PublicParFun.GetUriBySMSRange(SMSRange.All);
        String[] projection = new String[] {field};
        Cursor cur = myContext.getContentResolver().query(uri, 
                              projection,
                              "1=1 ) group by ( thread_id", null, "max(address)");
        listbyField=FillListField(cur,field);
        cur.close();

        return listbyField;
  }


   private static List<String> FillListField(Cursor cur,String field){
        List<String> mListField=new ArrayList<String>();

        while(cur.moveToNext()){        
            String s=cur.getString(cur.getColumnIndex(field));
            if (s!=null) {
                mListField.add(s);
            }
        }

        return mListField;
   }


    public static Uri GetUriBySMSRange(SMSRange mSMSRange){
        Uri uri=null;

        final String SMS_URI_ALL = "content://sms/";
        final String SMS_URI_INBOX = "content://sms/inbox";
        final String SMS_URI_SEND = "content://sms/sent";
        final String SMS_URI_OUTBOX = "content://sms/outbox";
        final String SMS_URI_DRAFT = "content://sms/draft";

        switch (mSMSRange){
            case All:
                uri = Uri.parse(SMS_URI_ALL);
                break;

            case Inbox:
                uri = Uri.parse(SMS_URI_INBOX);
                break;

            case Sentbox:
                uri = Uri.parse(SMS_URI_SEND);
                break;

            case Outbox:
                uri = Uri.parse(SMS_URI_OUTBOX);
                break;

            case Draft:
                uri = Uri.parse(SMS_URI_DRAFT);
                break;
        }

        return uri;
    }

Aucun commentaire:

Enregistrer un commentaire