Search code examples
androidlistviewandroid-intentnotificationslistactivity

android click notification to open listview's note


I have a notification set up inside an IntentService activity this way:

  // Build the content
  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  builder.setContentTitle(this.getResources().getString(R.string.Notification_Title));
  builder.setContentText(contextText);
  builder.setSmallIcon(R.drawable.ic_circle_white_36dp);
  builder.setTicker("ticker title");
  builder.setPriority(NotificationCompat.PRIORITY_HIGH);
  builder.setAutoCancel(true);

  // Provide the Explicit intent
  Intent in = new Intent(this, MainActivity.class);

  // Add the back stack using TaskBuilder and set the Intent to pending intent
  TaskStackBuilder stackbuilder = TaskStackBuilder.create(this);
  stackbuilder.addParentStack(MainActivity.class);
  stackbuilder.addNextIntentWithParentStack(in);
  PendingIntent pi_main = stackbuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

  builder.setContentIntent(pi_main);

  // Notification through notification Manager
  Notification notification = builder.build();
  NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
  manager.notify(1235, notification);

and a MainActivity that extends ListActivity with a listview, with the following code to manage a single cell click:

OnItemClickListener viewNoteListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {

        // Open ViewNote activity
        Intent viewnote = new Intent(MainActivity.this, ViewNote.class);

        // Pass the ROW_ID to ViewNote activity
        viewnote.putExtra(ROW_ID, arg3);
        startActivity(viewnote);
    }
};

which is called inside onCreate() with:

    noteListView = getListView();
    noteListView.setOnItemClickListener(viewNoteListener);

Inside the IntentService activity I'm retrieving the cell title (cell is composed by id and title)

Q. Is there a way I can open a certain listview cell (knowing it's title) clicking the notification?

EDIT: ViewNote.class is:

public class ViewNote extends Activity {

// Declare Variables
private long rowID;
private TextView TitleTv;
private TextView NoteTv;
Button btnEditing;
Button btnDel;
Button btnBack;
private static final String TITLE = "title";
private static final String NOTE = "note";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_note);

    // Locate the TextView in view_note.xml
    TitleTv = (TextView) findViewById(R.id.TitleText);
    NoteTv = (TextView) findViewById(R.id.NoteText);
    btnEditing = (Button) findViewById(R.id.noteEditing);
    btnDel = (Button) findViewById(R.id.noteDel);
    btnBack = (Button) findViewById(R.id.gobackFakeTwo);

    // Retrieve the ROW ID from MainActivity.java
    Bundle extras = getIntent().getExtras();
    rowID = extras.getLong(MainActivity.ROW_ID);

    btnBack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View emptyView) {
            finish();
        }
    });

    btnDel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View emptyView) {
            DeleteNote();
        }
    });

    btnEditing.setOnClickListener(new View.OnClickListener() {
        public void onClick(View emptyView) {
            Intent addeditnotes = new Intent(ViewNote.this, AddEditNotes.class);

            addeditnotes.putExtra(MainActivity.ROW_ID, rowID);
            addeditnotes.putExtra(TITLE, TitleTv.getText());
            addeditnotes.putExtra(NOTE, NoteTv.getText());
            startActivity(addeditnotes);
        }
    });
}

@Override
protected void onResume() {
    super.onResume();

    // Execute LoadNotes() AsyncTask
    new LoadNotes().execute(rowID);
}

// LoadNotes() AsyncTask
private class LoadNotes extends AsyncTask<Long, Object, Cursor> {
    // Calls DatabaseConnector.java class
    DatabaseConnector dbConnector = new DatabaseConnector(ViewNote.this);

    @Override
    protected Cursor doInBackground(Long... params) {
        // Pass the Row ID into GetOneNote function in
        // DatabaseConnector.java class
        dbConnector.open();
        return dbConnector.GetOneNote(params[0]);
    }

    @Override
    protected void onPostExecute(Cursor result) {
        super.onPostExecute(result);

        result.moveToFirst();
        // Retrieve the column index for each data item
        int TitleIndex = result.getColumnIndex(TITLE);
        int NoteIndex = result.getColumnIndex(NOTE);

        // Set the Text in TextView
        TitleTv.setText(result.getString(TitleIndex));
        NoteTv.setText(result.getString(NoteIndex));

        result.close();
        dbConnector.close();
    }
}

// Create an options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Edit Note")
            .setOnMenuItemClickListener(this.EditButtonClickListener)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    menu.add("Delete Notes")
            .setOnMenuItemClickListener(this.DeleteButtonClickListener)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return super.onCreateOptionsMenu(menu);
}

// Capture edit menu item click
OnMenuItemClickListener EditButtonClickListener = new OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        // Pass Row ID and data to AddEditNotes.java
        Intent addeditnotes = new Intent(ViewNote.this, AddEditNotes.class);

        addeditnotes.putExtra(MainActivity.ROW_ID, rowID);
        addeditnotes.putExtra(TITLE, TitleTv.getText());
        addeditnotes.putExtra(NOTE, NoteTv.getText());
        startActivity(addeditnotes);

        return false;

    }
};

// Capture delete menu item click
OnMenuItemClickListener DeleteButtonClickListener = new OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        // Calls DeleteNote() Function
        DeleteNote();

        return false;

    }
};

private void DeleteNote() {

    // Display a simple alert dialog to reconfirm the deletion
    AlertDialog.Builder alert = new AlertDialog.Builder(ViewNote.this);
    alert.setTitle("Delete note");
    alert.setMessage("Do you really want to delete this note?");

    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int button) {
            final DatabaseConnector dbConnector = new DatabaseConnector(
                    ViewNote.this);

            AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {
                @Override
                protected Object doInBackground(Long... params) {
                    // Passes the Row ID to DeleteNote function in
                    // DatabaseConnector.java
                    dbConnector.DeleteNote(params[0]);
                    return null;
                }

                @Override
                protected void onPostExecute(Object result) {
                    // Close this activity
                    finish();
                }
            };
            // Execute the deleteTask AsyncTask above
            deleteTask.execute(new Long[] { rowID });
        }
    });

    // Do nothing on No button click
    alert.setNegativeButton("No", null).show();
}

}


Solution

  • There is no need to launch MainActivity on notification click when you have a specific activity to show one item. The only thing required is to know the id of item to be displayed while clicking the notification.

    Change

    Intent in = new Intent(this, MainActivity.class);
    

    to

    Intent in = new Intent(this, ViewNote.class);
    in.putExtra(ROW_ID, arg3); // While clicking the notification you need to know which item to open