gpt4 book ai didi

android点击通知打开listview的笔记

转载 作者:行者123 更新时间:2023-11-29 01:24:14 25 4
gpt4 key购买 nike

我通过这种方式在 IntentService Activity 中设置了一个通知:

  // 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 Activity 中,我正在检索单元格标题(单元格由 ID 和标题组成)

问。有没有一种方法可以打开某个 ListView 单元格(知道它的标题)点击通知?

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

最佳答案

当您有特定 Activity 来显示一项时,无需在通知点击时启动 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

关于android点击通知打开listview的笔记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com