public void requestDelete(BaseContainer view) { try { // For list items, getParent twice will get its BaseContainer. if (view.getParent().getParent() instanceof BaseContainer) { BaseContainer container = (BaseContainer) view.getParent().getParent(); int index = this.indexOfChild(container); // if (index < 1){ // return; // } // final BaseContainer toDel = (BaseContainer) this.getChildAt(index - 1); // if (container.getType() == Constants.TYPE_TODO && toDel.getType() == // Constants.TYPE_TODO){ // this.removeView(toDel); // } else { this.removeView((View) view.getParent().getParent()); this.addView(new Text(getContext()), index); // } return; } } catch (NullPointerException e) { // Eat it. } int index = this.indexOfChild(view); if (index < 1) { return; } final BaseContainer toDel = (BaseContainer) this.getChildAt(index - 1); if (toDel.getType() == Constants.TYPE_TEXT && toDel.isEmpty()) { removeView(toDel); return; } if (view.getType() == Constants.TYPE_TEXT && view.isEmpty() && index != getChildCount() - 1) { removeView(view); return; } if (toDel.getType() == Constants.TYPE_TODO) { removeView(toDel); return; } String which = null; switch (toDel.getType()) { case Constants.TYPE_IMAGE: which = "Image"; break; case Constants.TYPE_ATT: which = "File"; break; case Constants.TYPE_REC: which = "Voice"; break; } if (which == null) { return; } new AlertDialog.Builder(getContext()) .setTitle("Confirm") .setMessage("Delete " + which + " ?") .setPositiveButton( "Sure", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditView.this.removeView(toDel); dialog.dismiss(); } }) .setNegativeButton( "No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create() .show(); }
public String exportJSON(String where) { File file = new File(where); if (!file.exists()) { file.mkdirs(); } file = new File(where + "content.json"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } ArrayList<Object> content = new ArrayList<>(); Gson gson = new Gson(); BaseContainer e; int empty = 0; for (int i = 0; i < getChildCount(); i++) { e = (BaseContainer) getChildAt(i); switch (e.getType()) { case Constants.TYPE_TEXT: if (!e.isEmpty()) { content.add(((ElementBean) e.getJsonBean()).setIndex(i - empty)); } else { empty++; } break; case Constants.TYPE_IMAGE: if (!e.isEmpty()) { content.add(((ElementBean) e.getJsonBean()).setIndex(i - empty)); } else { empty++; } break; case Constants.TYPE_TODO: if (!e.isEmpty()) { content.add(((ElementBean) e.getJsonBean()).setIndex(i - empty)); } else { empty++; } break; case Constants.TYPE_ATT: if (!e.isEmpty()) { content.add(((ElementBean) e.getJsonBean()).setIndex(i - empty)); } else { empty++; } break; case Constants.TYPE_ITEM: if (!e.isEmpty()) { content.add(((ElementBean) e.getJsonBean()).setIndex(i - empty)); } else { empty++; } break; default: break; } } FileWriter dos = null; try { dos = new FileWriter(file); dos.write(gson.toJson(content)); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { if (dos != null) try { dos.close(); } catch (IOException e1) { e1.printStackTrace(); } } return file.getName(); }