Note that there are some explanatory texts on larger screens.

plurals
  1. PORefresh ListView when Device receives GCM IntentService Message
    primarykey
    data
    text
    <p>My app is able to receive messages from GCM and saves the messages to the SQLlite database on the phone. The messages are viewable in a activity that has a listview. </p> <p>In the code below, the onPause() function refreshes the listView. This is not a good implementation because it only works if the activity is not displayed at the time of the update. If the activity is displayed at the time of an update, the list is static and does not update. </p> <p>Questions:</p> <ol> <li><p>How to I update the listview when the activity is being displayed? Or is there a way to use a background service to update the adapter, so that whenever the activity is displayed, it always shows the newest data.</p></li> <li><p>is this kind of functionality currently not possible with android and I'll need to implement something else like 'pull-to-refresh'?</p></li> </ol> <p><strong>refreshing listview in OnResume() crashes the application, and shows a null pointer exception.</strong> </p> <p>Activity:</p> <pre class="lang-java prettyprint-override"><code>public class NotesView extends Activity implements OnItemClickListener { ListView listView; NoteAdapter objAdapter; NotificationsDatabase db = new NotificationsDatabase(this); List&lt;Notes&gt; listAlerts; String note; String time; TextView noteView; TextView timeView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.note); listView = (ListView) findViewById(R.id.notelist); listView.setOnItemClickListener(this); noteView = (TextView) findViewById(R.id.noteDisplay); timeView = (TextView) findViewById(R.id.notetimeStampDisplay); new MyTask().execute(); } // My AsyncTask start... class MyTask extends AsyncTask&lt;Void, Void, List&lt;Notes&gt;&gt; { ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(NotesView.this); pDialog.setMessage("Loading..."); pDialog.setCancelable(false); pDialog.show(); if (isCancelled()) { this.cancel(true); } } @Override protected List&lt;Notes&gt; doInBackground(Void... params) { db.open(); listAlerts = db.getData(); if (isCancelled()) { this.cancel(true); } return null; } protected void onPostExecute(List&lt;Notes&gt; alerts) { if (null != pDialog &amp;&amp; pDialog.isShowing()) { pDialog.dismiss(); } db.close(); setAdapterToListview(); } }// end myTask public void setAdapterToListview() { objAdapter = new NoteAdapter(NotesView.this, R.layout.row_notes, listAlerts); objAdapter.sortByNoteDesc(); objAdapter.notifyDataSetChanged(); listView.setAdapter(objAdapter); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) { Intent intent = new Intent( NotesView.this.getApplicationContext(), TabBarExample.class); intent.putExtra("goToTab", "Alerts"); startActivity(intent); return true; } return super.onKeyDown(keyCode, event); } public void onItemClick(AdapterView&lt;?&gt; parent, View viewDel, int position, long id) { for (int i = 0; i &lt; 1; i++) { Notes item = listAlerts.get(position); int ids = item.getId(); note = item.getNote(); time = item.getTimeStamp(); } System.out.println(note + " " + time); // } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); setContentView(R.layout.note); listView = (ListView) findViewById(R.id.notelist); listView.setAdapter(null); listView.setOnItemClickListener(this); noteView = (TextView) findViewById(R.id.noteDisplay); timeView = (TextView) findViewById(R.id.notetimeStampDisplay); new MyTask().execute(); } @Override protected void onDestroy() { } } </code></pre> <p>Code snippets From GCMIntentService</p> <pre class="lang-java prettyprint-override"><code>@Override protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received message"); //String message = getString(R.string.gcm_message); System.out.println("onMessage: "); Bundle extras = intent.getExtras(); String message = extras.getString("message"); String event_id_from_server = extras.getString("server_id"); // displayMessage(context, message); generateNotification(context, message); saveMsg(message); System.out.println("server id is " + event_id_from_server); if (event_id_from_server != null) { updateLocalDatabase(event_id_from_server); } } public void saveMsg(String msg) { boolean worked = true; try { NotificationsDatabase entry = new NotificationsDatabase(GCMIntentService.this); entry.open(); java.util.Date date = new java.util.Date(); Timestamp x = new Timestamp(date.getTime()); String timeStamp = x.toLocaleString(); entry.createEntry(msg, timeStamp); entry.close(); //update adapter service } catch (Exception e) { worked = false; String error = e.toString(); System.out.println(error); } finally { if (worked) { } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload