android点击通知打开listview的注释

时间:2016-01-25 13:32:09

标签: android listview android-intent notifications listactivity

我在 IntentService活动中设置了通知:

  // 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);

以及使用listview扩展ListActivity的 MainActivity ,使用以下代码管理单个单元格点击:

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);
    }
};

在onCreate()内部调用:

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

在IntentService活动中我正在检索单元格标题(单元格由id和title组成)

问。有没有办法可以点击通知打开某个列表视图单元格(知道它的标题)?

编辑:ViewNote.class是:

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();
}

}

1 个答案:

答案 0 :(得分:1)

当您有特定活动来展示一个项目时,无需在通知点击时启动MainActivity。唯一需要的是在点击通知时知道要显示的项目的ID。

更改

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

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