是否需要重新启动应用以显示更改?

时间:2018-06-28 03:57:30

标签: android

我正在开发第一个Project Notes应用程序。我遇到了一个问题,我已经成功创建了数据库,并且能够保存数据库中的数据并从数据库中检索数据。我面临的问题是一旦保存便笺,就需要重新启动应用程序才能显示它。如果我能得到任何帮助,那将非常好

public class MainActivity extends AppCompatActivity {

    private DatabaseHelper db;
    private List<Note> noteList=new ArrayList<>();
    private RecyclerView recyclerView;
    private NotesAdaptor notesAdaptor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //function to get all list
        db = new DatabaseHelper(this);
        SQLiteDatabase sqLiteDatabase=db.getReadableDatabase();
        noteList.addAll(db.getAllNotes(sqLiteDatabase));
        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        notesAdaptor=new NotesAdaptor(this,noteList);
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(notesAdaptor);
        recyclerView.addOnItemTouchListener(new NotesLongClickListener(this, recyclerView, new NotesLongClickListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {

            }

            @Override
            public void onLongClick(View view, int position) {
                showActionDialog(position);
            }
        }));
        FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNoteDialog(false,null,-1);

            }
        });

    }


    private void deleteNote(int position){

        db.deleteNote(noteList.get(position));
        notesAdaptor.notifyItemRemoved(position);

    }


    private void showActionDialog(final int postion){
        CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose option");
        builder.setItems(colors, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    Toast.makeText(MainActivity.this,"Clicked",Toast.LENGTH_SHORT).show();
                } else {
                    Log.d("Database Operations","Note deleted");
                    deleteNote(postion);
                }
            }
        });
        builder.show();
    }

    private void addNote(String s){
        DatabaseHelper db=new DatabaseHelper(this);
        SQLiteDatabase database=db.getWritableDatabase();
        db.createNote(s,database);
        db.close();
        notesAdaptor.notifyDataSetChanged();

        Toast.makeText(MainActivity.this,"Note Added",Toast.LENGTH_SHORT).show();
    }

    private void showNoteDialog(final boolean update, final Note note, final int position){
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
        View view=inflater.inflate(R.layout.add_notes,null);
        builder.setView(view);


        final EditText inputNote=view.findViewById(R.id.add_note_dailog);
        TextView dailogTitle =view.findViewById(R.id.dialog_itle);
        dailogTitle.setText(!update?"New Note":"Update Note");

        if(update &&  note!=null){
            inputNote.setText(note.getNote());
        }
        builder.setCancelable(false)
                .setPositiveButton(update ? "update" : "save", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        final AlertDialog  alertDialog=builder.create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(inputNote.getText().toString())) {
                    Toast.makeText(MainActivity.this, "Enter note!", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    alertDialog.dismiss();
                }
                if (update && note != null) {
                    // update note by it's id
                   // update(inputNote.getText().toString(), position);
                } else {
                    // create new note
                    addNote(inputNote.getText().toString());
                }
            }

        });



    }




}

1 个答案:

答案 0 :(得分:2)

  

是否需要重新启动应用才能显示更改?

您正面临此问题,因为在执行添加和从数据库中删除项目的操作时,同时没有在列表中执行任何操作,因此您需要重新启动应用程序

您的代码中有两个问题

  

问题1 :在您的deleteNote()方法中,您没有从列表中删除项目

private void deleteNote(int position){

    // remove item from your list than use notesAdaptor.notifyItemRemoved(position);    
    noteList.remove(s); 

    db.deleteNote(noteList.get(position));
    notesAdaptor.notifyItemRemoved(position);

}
  

问题2 :在您的addNote()方法中,您没有在列表中添加项目

private void addNote(String s){
    DatabaseHelper db=new DatabaseHelper(this);
    SQLiteDatabase database=db.getWritableDatabase();
    db.createNote(s,database);
    db.close();

    // add item in your list than use notesAdaptor.notifyDataSetChanged();
    noteList.add(s);
    notesAdaptor.notifyDataSetChanged();


    Toast.makeText(MainActivity.this,"Note Added",Toast.LENGTH_SHORT).show();
}