Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从关联菜单中删除单个列表项时出错_Java_Android - Fatal编程技术网

Java 从关联菜单中删除单个列表项时出错

Java 从关联菜单中删除单个列表项时出错,java,android,Java,Android,我正在处理一个便条应用程序。我实现了一个NoteAdapter类来显示列表项和可序列化的note。我试图在显示警报对话框后从关联菜单中删除单个列表视图项。现在我遇到了车祸,不知道我做错了什么 主要活动 public boolean onContextItemSelected(final MenuItem item) { final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterConte

我正在处理一个便条应用程序。我实现了一个NoteAdapter类来显示列表项和可序列化的note。我试图在显示警报对话框后从关联菜单中删除单个列表视图项。现在我遇到了车祸,不知道我做错了什么

主要活动

    public boolean onContextItemSelected(final MenuItem item) {
            final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
          final  Note mLoadedNote = (Note) mListNotes.getAdapter().getItem(info.position);
            mLoadedNote.getTitle();
            switch (item.getItemId()) {

                case R.id.delete:
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                            .setTitle("Delete " +  mLoadedNote.getTitle())
                            .setMessage("are you sure?")
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        here is where am getting the error
                        if(mLoadedNote != null) {
                            ArrayAdapter<String> note = (ArrayAdapter<String>) mListNotes.getAdapter().getItem(info.position);
    note.remove(note.getItem(info.position));
                            note.notifyDataSetChanged();
                            Toast.makeText(MainActivity.this,  mLoadedNote.getTitle() + " is deleted", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(MainActivity.this,  "can not delete the note '" + mLoadedNote.getTitle() + "'", Toast.LENGTH_SHORT).show();
                        }

                    }
                    })
                        .setNegativeButton("NO", null); //do nothing on clicking NO button :P
                    alertDialog.show();
            }
            return super.onContextItemSelected(item);
        }
错误

更改此项:

ArrayAdapter<String> note = (ArrayAdapter<String>) mListNotes.getAdapter().getItem(info.position);
ArrayAdapter note=(ArrayAdapter)mListNotes.getAdapter().getItem(info.position);
为此:

ArrayAdapter<Note> note = (ArrayAdapter<Note>) mListNotes.getAdapter();
ArrayAdapter note=(ArrayAdapter)mListNotes.getAdapter();

另外,使用
note
作为ArrayDater变量的名称也不好。请将其重命名为
notesArrayAdapter
,或类似的名称。

您能否证明更改的合理性,以便我们更好地理解删除列表视图项的逻辑。但是,当我保存一个新的注释项时,当列表视图更新时,删除的注释项会不断返回。
FATAL EXCEPTION: main
                                                                       Process: com.app.ben.notetaker, PID: 27476
                                                                       java.lang.ClassCastException: com.app.ben.notetaker.Note cannot be cast to android.widget.ArrayAdapter
                                                                           at com.app.ben.notetaker.MainActivity$2.onClick(MainActivity.java:103)
                                                                           at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
ArrayAdapter<String> note = (ArrayAdapter<String>) mListNotes.getAdapter().getItem(info.position);
ArrayAdapter<Note> note = (ArrayAdapter<Note>) mListNotes.getAdapter();