0

我的应用程序能够从 GCM 接收消息并将消息保存到手机上的 SQLlite 数据库。这些消息在具有列表视图的活动中是可见的。

在下面的代码中,onPause() 函数刷新了 listView。这不是一个好的实现,因为它仅在更新时未显示活动时才有效。如果活动在更新时显示,则列表是静态的并且不会更新。

问题:

  1. 显示活动时如何更新列表视图?或者有没有办法使用后台服务来更新适配器,这样每当显示活动时,它总是显示最新的数据。

  2. 这种功能目前在 android 上是不可能的,我需要实现一些其他的东西,比如“pull-to-refresh”?

在 OnResume() 中刷新 listview 会使应用程序崩溃,并显示空指针异常。

活动:

public class NotesView extends Activity implements OnItemClickListener {


    ListView listView;
    NoteAdapter objAdapter;
    NotificationsDatabase db = new NotificationsDatabase(this);
    List<Notes> 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<Void, Void, List<Notes>> {

        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<Notes> doInBackground(Void... params) {


            db.open();
            listAlerts = db.getData();

            if (isCancelled()) {

                this.cancel(true);
            }


            return null;
        }

        protected void onPostExecute(List<Notes> alerts) {


            if (null != pDialog && 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 && 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<?> parent, View viewDel, int position,
                            long id) {


        for (int i = 0; i < 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() {

    }


}

来自 GCMIntentService 的代码片段

@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) {


        }

    }
}
4

1 回答 1

3

我稍微清理了你的代码。基本上所有的视图分配都应该在 onCreate 中完成一次,而数据的加载应该在 onResume() 中完成。看看这是否有帮助:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   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);
}

@Override
protected void onResume() {
   super.onResume();
   new MyTask().execute();
}

@Override
   protected void onPause() {
   super.onPause();
}
于 2012-10-17T21:23:48.337 回答