2017-08-05 121 views
-1

我正在處理一個筆記應用程序。我已經實現了一個筆記Adapter類來顯示列表項和Note Serializable。我想在顯示警報對話框後從上下文菜單中刪除單個列表視圖項目。現在我得到崩潰,不知道什麼是做錯了。從上下文菜單中刪除單個列表項時出錯

主要活動

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); 
     } 

注意適配器

public class NoteAdapter extends ArrayAdapter<Note> { 

    public static final int WRAP_CONTENT_LENGTH = 50; 
    public NoteAdapter(Context context, int resource, List<Note> objects) { 
     super(context, resource, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if(convertView == null) { 
      convertView = LayoutInflater.from(getContext()) 
        .inflate(R.layout.view_note_item, null); 
     } 

     Note note = getItem(position); 

     if(note != null) { 
      TextView title = (TextView) convertView.findViewById(R.id.list_note_title); 
      TextView date = (TextView) convertView.findViewById(R.id.list_note_date); 
      TextView content = (TextView) convertView.findViewById(R.id.list_note_content_preview); 

      title.setText(note.getTitle()); 
      date.setText(note.getDateTimeFormatted(getContext())); 

      //correctly show preview of the content (not more than 50 char or more than one line!) 
      int toWrap = WRAP_CONTENT_LENGTH; 
      int lineBreakIndex = note.getContent().indexOf('\n'); 
      //not an elegant series of if statements...needs to be cleaned up! 
      if(note.getContent().length() > WRAP_CONTENT_LENGTH || lineBreakIndex < WRAP_CONTENT_LENGTH) { 
       if(lineBreakIndex < WRAP_CONTENT_LENGTH) { 
        toWrap = lineBreakIndex; 
       } 
       if(toWrap > 0) { 
        content.setText(note.getContent().substring(0, toWrap) + "..."); 
       } else { 
        content.setText(note.getContent()); 
       } 
      } else { //if less than 50 chars...leave it as is :P 
       content.setText(note.getContent()); 
      } 
     } 

     return convertView; 
    } 

} 

Note.java

public class Note implements Serializable { 
    private long mDateTime; //creation time of the note 
    private String mTitle; //title of the note 
    private String mContent; //content of the note 

    public Note(long dateInMillis, String title, String content) { 
     mDateTime = dateInMillis; 
     mTitle = title; 
     mContent = content; 
    } 

    public void setDateTime(long dateTime) { 
     mDateTime = dateTime; 
    } 

    public void setTitle(String title) { 
     mTitle = title; 
    } 

    public void setContent(String content) { 
     mContent = content; 
    } 

    public long getDateTime() { 
     return mDateTime; 
    } 

    /** 
    * Get date time as a formatted string 
    * @param context The context is used to convert the string to user set locale 
    * @return String containing the date and time of the creation of the note 
    */ 
    public String getDateTimeFormatted(Context context) { 
     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss" 
       , context.getResources().getConfiguration().locale); 
     formatter.setTimeZone(TimeZone.getDefault()); 
     return formatter.format(new Date(mDateTime)); 
    } 

    public String getTitle() { 
     return mTitle; 
    } 

    public String getContent() { 
     return mContent; 
    } 
} 

錯誤

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) 

回答

0

更改此:

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

這樣:

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

此外,它不好用note作爲ArrayAdater變種的名稱。將它重命名爲notesArrayAdapter或類似的東西。

+0

你能證明這個改變,所以我們可以更好地理解 –

+0

這個邏輯可以刪除一個列表視圖項。然而,當我保存一個新的筆記項目時,刪除的項目會在列表視圖更新時繼續回來。 – benruty

相關問題