0

I currently have a basic program that runs through all the SMS and creates a Thread for each one only storing the name, number, and last text. It then creates a listview out of all these threads (like the stock sms app)... But it runs really slow; how could I make this more efficient so it doesn't take minutes each time it boots?

Uri message = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();

Cursor c = cr.query(message, null, null, null, null);
this.startManagingCursor(c);
int totalSMS = c.getCount();
        if (c.moveToFirst()) {
         for (int i = 0; i < totalSMS; i++) {
                String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup._ID};
                Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(c.getString(c.getColumnIndexOrThrow("address"))));
                Cursor cursor2 = this.getContentResolver().query(contactUri, projection, null, null, null);
                String name = getContactName(this, c.getString(c.getColumnIndexOrThrow("address")));
                String number = c.getString(c.getColumnIndexOrThrow("address"));
                String msg =c.getString(c.getColumnIndexOrThrow("body"));
                if (cursor2.moveToFirst()) {

                    // Get values from contacts database:
                    //contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
                    name =      cursor2.getString(cursor2.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                }

                //entries.add(new Entry(name, msg));
                int newNumber = 1;
                for(int d = 0; d < threads.size(); d++)
                {
                    Thread t = threads.get(d);
                    if(t.number.equals(number))
                        newNumber = 0;
                }
                if(newNumber == 1)
                    threads.add(new Thread(name, number, msg));

                c.moveToNext();
            }
        }
Jister13
  • 149
  • 1
  • 13

2 Answers2

0

You can achieve this by using endless Adapter implementation. This exactly does what you want. You can also restrict the number of rows to be refreshed per scroll. Here is a link to it.,

Android: Implementing Endless List like Android Market

https://github.com/commonsguy/cwac-endless

To use it, you extend EndlessAdapter to provide details about how to handle the endlessness. Specifically, you need to be able to provide a row View, independent from any of the rows in your actual adapter, that will serve as a placeholder while you, in another method, load in the actual data to your main adapter. Then, with a little help from you, it seamlessly transitions in the new data.

Or You can create a simple lazyAdapter which will load the data when required.

Update 1:

Effectively Load the data from the database use the Cursor loader. Here is an sample code

Community
  • 1
  • 1
kAnNaN
  • 3,669
  • 4
  • 28
  • 39
  • This is what I need for each thread, but currently I am talking out simply showing the theads when you first launch the app. It'll show a listview of each contact thread.... – Jister13 Apr 05 '14 at 14:29
  • Then you could probably use CusorAdapter and cusorloader. check update 1 for references. – kAnNaN Apr 05 '14 at 14:39
0

Don't read all the columns from the cursor. Just read the cursor what are the columns you want.

Sample

cr.query(uri, new String[]{"address","body"},null, null, null);

Note
And while dealing with cursors dont forgot to close the cursors
would reduce the time by 50% but not complete.

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29