【问题标题】:Android how to get ListView ID and using it out of onListItemClick? ListView, Notification, SQLite databaseAndroid 如何从 onListItemClick 中获取 ListView ID 并使用它? ListView、通知、SQLite 数据库
【发布时间】:2015-05-02 13:24:04
【问题描述】:

我有一个 ListView,它从 SQLite 数据库中填充注释。以下代码的效果是,当用户从 ListView 中选择任何项目时,应用程序将访问其中包含数据的 NoteEdit.class。我把这段代码放在这里的原因是为了证明我的数据库创建是好的。

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 感谢您的提醒,它有效。那时我已经麻木了。

标签: android sqlite listview android-listview android-notifications


【解决方案1】:

intent.putExtra("卡车", id)

使用 另一个类中的 OnHandleIntent 方法并使用传递的意图并使用 .getExtras().getLong("truck") 作为长变量来使用

【讨论】:

  • 你的意思是我应该使用“intent.putExtra("truck", id)" 而不是"resultIntent.putExtra(NoteDbAdapter.KEY_ROWID, );"?在哪个类中使用 OnHandleIntent?你能解释一下吗?对不起,我是菜鸟。
  • 好的,我从@Squonk 的评论中找到了答案,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2017-09-12
相关资源
最近更新 更多