我有一個ListView,它從SQLite數據庫填充筆記。以下代碼的作用是,當用戶從ListView中選擇任何項目時,應用程序將訪問其中包含數據的NoteEdit.class。我在這裏放這段代碼的目的是爲了證明我的數據庫創建是正確的。Android如何獲取ListView ID並將其用於onListItemClick? ListView,通知,SQLite數據庫
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);
}
現在我有一個通知,它粘貼從ListView到通知欄的項目的註釋。通知和便箋也很好。但問題是,當我點擊通知項,就應該把用戶的EditNote.class包含內部的數據,這是通知中的代碼:
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;
我需要的東西,如「ID」來定位並從數據庫中放入數據,例如onListItemClick中的「i.putExtra(NoteDbAdapter.KEY_ROWID,id);」。我怎麼能實現它?
是不是你需要相同的ID爲'notice.id'? – Squonk
@Squonk感謝您的提醒,它的工作原理。那時我麻木了。 – SpainshCake