【问题标题】:android click notification to open listview's noteandroid点击通知打开listview的笔记
【发布时间】:2016-05-01 20:09:24
【问题描述】:

我通过这种方式在 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);

和一个 MainActivity,它使用列表视图扩展 ListActivity,使用以下代码来管理单个单元格单击:

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 和标题组成)

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

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

}

【问题讨论】:

  • 当我们点击通知时,你想在listview中打开一个特定的列表项吗?
  • 是的,很抱歉我重新提出了这个问题,因为我忘了提及它。

标签: android listview android-intent notifications listactivity


【解决方案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

【讨论】:

  • 谢谢,这是第一步,我重新提出了问题,因为我忘了提到单元格必须是我知道标题的特定单元格
  • @m4tt:一般情况下,您在下一个活动中使用其在列表视图中的位置打开特定项目。我建议保留另一个值(如唯一 ID)并使用它来打开新活动。这在这两种情况下都会对您有所帮助(在有或没有通知的情况下打开项目)。
  • 我确实有行的 id,所以现在对我来说最简单的方法是,因为在 IntentService 中检索该标题值需要进行复杂的计算,所以使用它的标题获取单元格的 id,这可能吗?
  • 出于好奇:将 id 保留在 Intent 服务中并据此获取标题会不会更好。将独特的价值掌握在手中总是更好。此外,根据对象的结构,可能/可能无法获取 id。如果标题重复怎么办?
  • 那是真的,作为第一步,我需要更改 fetch 以查找 id。然后我想我可以找到有关通过堆栈上的 id 获取标题的线程。之后我只需要放置in.putExtra(ROW_ID); 对吗? (不确定 arg3 是什么)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2011-11-07
相关资源
最近更新 更多