Advertisement
sweetpoision

AsyncTask class for stackoverflow question : https://stackoverflow.com/questions/63176435/activity-does-not-respond-and-recyclerview-not-reflecting-changes

Jul 31st, 2020 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. private static class ReadDbTableAllAsyncTask extends AsyncTask<ArrayList<String>, Void, ArrayList<String>>{
  2. private static final String TAG = "[MY_DEBUG]";
  3.  
  4. private WeakReference<MainActivity> activityWeakReference;
  5. private SQLiteDatabase db1;
  6. private long milli_seconds = 0;
  7. private Calendar calendar = Calendar.getInstance();
  8. private DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy h:mm a");
  9. private String printable_date;
  10. private int i=0;
  11. private ArrayList<String> messages_list = new ArrayList();
  12. private Cursor cursor_read_from_table_all;
  13. private int progress_iterator;
  14. private boolean cursor_first;
  15.  
  16. ReadDbTableAllAsyncTask(MainActivity activity, SQLiteDatabase db2){
  17. activityWeakReference = new WeakReference<MainActivity>(activity);
  18. db1 = db2;
  19. i=0;
  20. }
  21.  
  22. @Override
  23. protected ArrayList<String> doInBackground(ArrayList<String>... arrayList) {
  24. db1.beginTransaction();
  25. String[] projection = {
  26. BaseColumns._ID,
  27. SpamBusterContract.TABLE_ALL.COLUMN_CORRES_INBOX_ID,
  28. SpamBusterContract.TABLE_ALL.COLUMN_SMS_BODY,
  29. SpamBusterContract.TABLE_ALL.COLUMN_SMS_ADDRESS,
  30. SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE
  31. };
  32.  
  33. String selection = null;
  34. String[] selectionArgs = null;
  35.  
  36. String sortOrder =
  37. SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE + " desc "; //latest one appears on top of array_adapter
  38.  
  39.  
  40. cursor_read_from_table_all = db1.query(
  41. SpamBusterContract.TABLE_ALL.TABLE_NAME, // The table to query
  42. projection, // The array of columns to return (pass null to get all)
  43. selection, // The columns for the WHERE clause
  44. selectionArgs, // The values for the WHERE clause
  45. null, // don't group the rows
  46. null, // don't filter by row groups
  47. sortOrder // The sort order
  48. );
  49.  
  50. i = 0;
  51. cursor_first = cursor_read_from_table_all.moveToFirst();
  52. if (!cursor_first) {
  53. Log.d(TAG, "ReadDbTableAllAsyncTask: doInBackground(): TABLE_ALL is empty");
  54. }
  55. else {
  56. Log.d(TAG, "ReadDbTableAllAsyncTask doInBackground: interator i = " + i);
  57. int index_id = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL._ID);
  58. int index_corres_id = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_CORRES_INBOX_ID);
  59. int index_sms_body = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_BODY);
  60. int index_sms_address = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_ADDRESS);
  61. int index_sms_epoch_date = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE);
  62. try {
  63. messages_list.clear();
  64. do {
  65. long itemId = cursor_read_from_table_all.getLong(index_id);
  66. String corress_inbox_id = cursor_read_from_table_all.getString(index_corres_id);
  67. String sms_body = cursor_read_from_table_all.getString(index_sms_body);
  68. String sms_address = cursor_read_from_table_all.getString(index_sms_address);
  69. String epoch_date = cursor_read_from_table_all.getString(index_sms_epoch_date);
  70. Log.d(TAG, " doInBackground() : itemId = " + itemId);
  71. Log.d(TAG, " doInBackground() : corress_inbox_id = " + corress_inbox_id);
  72. Log.d(TAG, " doInBackground() : sms_body = " + sms_body);
  73. Log.d(TAG, " doInBackground() : sms_address = " + sms_address);
  74. milli_seconds = Long.parseLong(epoch_date);
  75. calendar.setTimeInMillis(milli_seconds);
  76. printable_date = formatter.format(calendar.getTime());
  77. Log.d(TAG, " doInBackground() : epoch_date = " + epoch_date + " which is : " + printable_date);
  78. Log.d(TAG, " doInBackground() : ");
  79. String str = "ItemID = " + itemId + "\ncorress_inbox_id = " + corress_inbox_id + "\n SMS From: " + getContactName(activityWeakReference.get(), sms_address) + "\n Recieved at: " + printable_date + "\n" + sms_body;
  80. Log.d(TAG, "doInBackground: progress_iterator = " + progress_iterator);
  81. Log.d(TAG, "doInBackground: i = " + i);
  82. Log.d(TAG, "doInBackground: adding into message_list at index + " + progress_iterator);
  83. Log.d(TAG, "doInBackground: messages_list.add(" + progress_iterator + ", " + str + ")");
  84. messages_list.add(progress_iterator++, str);
  85. Log.d(TAG, "ReadDbTableAllAsyncTask doInBackground: incrementing iterator i to " + ++i);
  86. } while (cursor_read_from_table_all.moveToNext());
  87. }
  88. catch(Exception e){
  89. Log.d(TAG, "doInBackground: exception : " + e);
  90. }
  91. }
  92. cursor_read_from_table_all.close();
  93. return messages_list;
  94. }
  95.  
  96. @Override
  97. protected void onPreExecute() {
  98. super.onPreExecute();
  99.  
  100. MainActivity activity = activityWeakReference.get();
  101. if (activity == null || activity.isFinishing()) {
  102. return;
  103. }
  104. messages_list.clear();
  105. }
  106.  
  107. @Override
  108. protected void onPostExecute(ArrayList<String> msg_list) {
  109. super.onPostExecute(msg_list);
  110. MainActivity activity = activityWeakReference.get();
  111. if (activity == null || activity.isFinishing()) {
  112. return;
  113. }
  114. int j=0;
  115. try {
  116. while (j < msg_list.size()) {
  117. activity.sms_adapter.insert(j, msg_list.get(j).toString());
  118. activity.sms_adapter.notifyDataSetChanged();
  119. j++;
  120. }
  121. }
  122. catch (Exception e){
  123. Log.d(TAG, "onPostExecute: exception : " + e);
  124. }
  125. Log.d(TAG, "onPostExecute: Finished reading TABLE_ALL");
  126. activity.messages.setAdapter(activity.sms_adapter);
  127. activity.messages.setLayoutManager(new LinearLayoutManager(activity));
  128. db1.startTransaction();
  129. db1.close();
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement