samedi 2 mai 2015

Android how to get ListView ID and using it out of onListItemClick? ListView, Notification, SQLite database

I have a ListView and it populates notes from SQLite database. The effect of the following code is that when user select any item from the ListView, the application will access to the NoteEdit.class which has data contains in it. The reason I put this code here is to prove my database creation is fine.

protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Intent i = new Intent(this, NoteEdit.class);
            i.putExtra(NoteDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);
    }

Now I have a Notification, it sticks the note which is the item from ListView to notification bar. The notification and stick note is also fine. But the problem is, when I click on the Notification item, it should bring user to the EditNote.class which contains data inside, this is the code in notification:

public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case STICKY_NOTIFICATION:
        String getTitle = "",
        getBodyText = "";
        AdapterContextMenuInfo notice = (AdapterContextMenuInfo) item
                .getMenuInfo();
        Cursor c = mDbHelper.fetchNote(notice.id);
        if (c.moveToFirst()) {
            do {
                getTitle = c.getString(1);
                getBodyText = c.getString(2);
            } while (c.moveToNext());
        }

        //Intent resultIntent = new Intent(this, NoteEdit.class);
        //resultIntent.putExtra(NoteDbAdapter.KEY_ROWID, ); //This is where the issue is
        //startActivityForResult(resultIntent, ACTIVITY_EDIT);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NoteEdit.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPending = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(getTitle).setContentText(getBodyText)
                .setContentIntent(resultPending);

        NotificationManager mNotificationManager;
        mNotificationManager = (NotificationManager) this
                .getApplicationContext().getSystemService(
                        Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(10, mBuilder.build());
        break;

I need something like "id" to locate and put data from the database, such as the one in onListItemClick "i.putExtra(NoteDbAdapter.KEY_ROWID, id);". How could I implement it?

Aucun commentaire:

Enregistrer un commentaire