0

I have one arraylist which have some contact number and i want to pass that arraylist to address. So that it would get all the messages from selected numbers which are in arraylist. Thank you in advance.

  StringBuilder smsBuilder = new StringBuilder();
    final String SMS_URI_INBOX = "content://sms/inbox";
    final String SMS_URI_ALL = "content://sms/";

    Uri uri = Uri.parse(SMS_URI_INBOX);
    String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };

    Cursor cur = getContentResolver().query(uri, projection, "address='"+list+"'" , null, "date desc");

    if (cur.moveToFirst())
    {
        int index_Address = cur.getColumnIndex("address");
        int index_Person = cur.getColumnIndex("person");
        int index_Body = cur.getColumnIndex("body");
        int index_Date = cur.getColumnIndex("date");
        int index_Type = cur.getColumnIndex("type");
        do
        {
            String strAddress = cur.getString(index_Address);
            int intPerson = cur.getInt(index_Person);
            String strbody = cur.getString(index_Body);
            long longDate = cur.getLong(index_Date);
            int int_Type = cur.getInt(index_Type);


            String str = "SMS From: " + cur.getString(index_Address) +
                    "\n" + cur.getString(index_Body) + "\n";
            arrayAdapter.add(str);

            smsBuilder.append("[ ");
            smsBuilder.append(strAddress + ", ");
            smsBuilder.append(intPerson + ", ");
            smsBuilder.append(strbody + ", ");
            smsBuilder.append(longDate + ", ");
            smsBuilder.append(int_Type);
            smsBuilder.append(" ]\n\n");
        } while (cur.moveToNext());

        if (!cur.isClosed())
        {
            cur.close();
            cur = null;
        }
    }
    else
    {
        smsBuilder.append("no result!");
    } // end if
}
Anjali Patel
  • 807
  • 2
  • 11
  • 23
  • Where is this `ArrayList` of numbers in your code? – pleft Oct 06 '17 at 08:19
  • Possible duplicate of [Get SMS of specific phone number](https://stackoverflow.com/questions/14721772/get-sms-of-specific-phone-number) – pleft Oct 06 '17 at 08:24
  • No, I am asking how to pass more than one contact number to address? @pleft – Anjali Patel Oct 06 '17 at 08:25
  • for len(arrayOfNumbers)=N ... `selection = createArraywithStringNTimes("address=?", N).join(" OR ")` and `selectionArgs = arrayOfNumbers` .... so `selection` will be `"address=? OR address=? OR address=? .... address=?"` and `selectionArgs = new String[] { "N1", "N2", "N3", ... "NN"}` – Selvin Oct 06 '17 at 08:34
  • sorry i m not getting this. Please be clear and put it into detail.Thanks @Selvin – Anjali Patel Oct 06 '17 at 08:40
  • It's because you didn't even ead `ContentResolver.query` documentation ... if you would then you would know what is `selection` and `selectionArgs` and how to use give **algorithm** (not code). also please be clear and put it into details what did you not understand ... – Selvin Oct 06 '17 at 08:43
  • Cursor cur = getContentResolver().query(uri, projection, "address = ?", arr_name, "date desc"); Please guide me. is this is right way? @Selvin – Anjali Patel Oct 06 '17 at 09:40
  • 1
    but if you want more addresses then you have to create selection like "address = ? OR address = ?" and then arr_name should contains the same elements as "?" in selection ... for 2 elements: "address = ? OR address = ?" for 3: "address = ? OR address = ? OR address = ?" and so on ... – Selvin Oct 06 '17 at 09:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156085/discussion-between-anjali-patel-and-selvin). – Anjali Patel Oct 06 '17 at 09:47

1 Answers1

0
       int argcount = MOBILENUMBER.size(); // number of IN arguments i.e More than one Mobile number for which you want read SMS

    //  String[] args = new String[]{ 1, 2 };
    StringBuilder inList = new StringBuilder(argcount * list.length);
    for (int i = 0; i < argcount; i++)
    {
        if(i > 0)
        {
            inList.append(",");
        }
        inList.append("?");//This will generate Number of "? " to match our item in array list
    }
    Cursor cur = getContentResolver().query(uri, projection, "address IN (" + inList.toString() + ")" , list, "date DESC ");
Anjali Patel
  • 807
  • 2
  • 11
  • 23