2016-11-03 18 views
0

CRUD操作でメモ帳アプリケーションを作成する。リストビューを更新するSQLiteの更新操作の後

ListViewの項目を更新しようとしています。この項目には、ノートのタイトルが含まれています。

コンテンツの値が新しい値を正常に表示しています。

しかし、ListViewは更新されたメモでは更新されず、古いものが表示され続けます。ここで

が更新さDBHandlerです:

public class DBhandler extends SQLiteOpenHelper { 
    private SQLiteDatabase db; 
    private static final int DATABASE_VERSION = 3; 
    private static final String DATABASE_NAME = "notes.db"; 
    public static final String TABLE_NAME = "notes"; 
    public static final String KEY_ID = "_id"; 
    public static final String DATA = "data"; 
    public static final String DATE = "date"; 
    public static final String TITLE = "title"; 
    String CREATE_TABLE = "create table " + TABLE_NAME + "(" + KEY_ID + " integer primary key autoincrement," 
      + DATA + " text not null," 
      + TITLE + " text not null, " 
      + DATE + " date " 
      + ") ; "; 

    public DBhandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     this.db = db; 
     db.execSQL(CREATE_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     this.db = db; 
     String DROP = "DROP TABLE IF EXISTS " + TABLE_NAME; 
     db.execSQL(DROP); 
     onCreate(db); 
    } 

    public void addNote(Note note) { 
     db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     String date = cal(); 
     values.put(DATA, note.getData()); 
     values.put(TITLE, note.getTitle()); 
     values.put(DATE, String.valueOf(date)); 
     db.insert(TABLE_NAME, null, values); 


    } 


    public void delete() { 
     db = getWritableDatabase(); 
     db.execSQL("DELETE FROM " + TABLE_NAME); 
    } 

    public void update(int id, String note, String title) { 

     String date = cal(); 
     db = this.getWritableDatabase(); 
    // db.rawQuery("UPDATE notes set _id=id,data=note,title=title,date=date where _id=id",null); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_ID, id); 
     values.put(DATA, note); 
     values.put(TITLE, title); 
     values.put(DATE, String.valueOf(date)); 
     // Log.d("ValueNew", values.toString()); 
     db.update(TABLE_NAME, values, KEY_ID + " = " + id, null); 
     Cursor cursor=db.rawQuery("SELECT * FROM notes",null); 
     cursor.moveToFirst(); 
     // Log.d("DATABASE",cursor.getString(0)+ " "+cursor.getString(1)+ " "+cursor.getString(2)); 

     // db.update(); 
    } 

    public void deleteItem(int id) { 
     String DEL = "DELETE FROM " + TABLE_NAME + " " + "WHERE _id = " + id; 
     db = getWritableDatabase(); 
     db.execSQL(DEL); 
    } 

    public String cal() { 
     Calendar ci = Calendar.getInstance(); 
     String date = " " + ci.get(Calendar.YEAR) + "-" + 
       (ci.get(Calendar.MONTH) + 1) + "-" + 
       ci.get(Calendar.DAY_OF_MONTH) + " " + 
       ci.get(Calendar.HOUR_OF_DAY) + ":" + 
       ci.get(Calendar.MINUTE); 
     return date; 
    } 


} 

ここで()の更新を呼び出すEditNoteクラスです。

public class ListNotesActivity extends AppCompatActivity { 
    ListView mListView; 
    Toolbar toolbar; 
    SimpleCursorAdapter simpleCursorAdapter; 
    DBhandler dBhandler; 
    Cursor c; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.noteslist); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     mListView = (ListView) findViewById(R.id.notesList); 
     registerForContextMenu(mListView); 
     display(); 
     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Cursor cursor = (Cursor) mListView.getItemAtPosition(position); 
       String data = (cursor.getString(1)); 
       Intent intent = new Intent(ListNotesActivity.this, ShowNote.class); 
       intent.putExtra("Note", data); 
       startActivity(intent); 


      } 
     }); 

    } 


    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
     int pos = adapterContextMenuInfo.position; 
     c = ((SimpleCursorAdapter) mListView.getAdapter()).getCursor(); 
     c.moveToPosition(pos); 

     switch (item.getItemId()) { 
      case R.id.update: 
       String title=c.getString(2); 
       String data=c.getString(1); 
       Intent intent=new Intent(ListNotesActivity.this,EditNote.class); 
       Bundle extras=new Bundle(); 
       extras.putInt("CursorId",pos); 
       extras.putString("newtitle",title); 
       extras.putString("newdata",data); 
       intent.putExtras(extras); 
       startActivity(intent); 
       Toast.makeText(this, "Update", Toast.LENGTH_SHORT).show(); 

       break; 
      case R.id.deleteItem: 
       dBhandler.getWritableDatabase(); 
       new DeleteItemClass().doInBackground(c); 
       break; 
     } 
     return super.onContextItemSelected(item); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.contextmenu, menu); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.listmenu, menu); 
     return true; 
    } 

     public void display() { 
      mListView = (ListView) findViewById(R.id.notesList); 
      dBhandler = new DBhandler(this); 
      SQLiteDatabase db = dBhandler.getReadableDatabase(); 
      Cursor cursor = db.rawQuery("SELECT * from " + DBhandler.TABLE_NAME, null); 
      String[] titles = new String[]{DBhandler.TITLE, DBhandler.DATE}; 
      simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.notetitle, cursor, titles, new int[]{R.id.noteTitleText, R.id.date}, 0); 
      // simpleCursorAdapter.notifyDataSetChanged(); 
      mListView.setAdapter(simpleCursorAdapter); 
      simpleCursorAdapter.notifyDataSetChanged(); 


     } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     display(); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     switch (id) { 
      case R.id.add: 
       Intent intent = new Intent(ListNotesActivity.this, SaveNoteActivity.class); 
       startActivity(intent); 
       break; 
      case R.id.delete: 
       dBhandler.getWritableDatabase(); 
       SQLiteDatabase db = dBhandler.getWritableDatabase(); 
       Cursor cursor = db.rawQuery("Select * from notes", null); 
       if (cursor.getCount() > 0) 
        dialog(); 
       else { 
        Toast.makeText(this, "No items to delete", Toast.LENGTH_SHORT).show(); 
       } 
       break; 
      case R.id.settings: 
       Intent intent1 = new Intent(ListNotesActivity.this, SettingsActivity.class); 
       startActivity(intent1); 
       break; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    public void dialog() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Confirm"); 
     builder.setMessage("Are You Sure to Delete?"); 
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       new DeleteClass().doInBackground(); 
      } 
     }); 
     builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
     AlertDialog alertDialog = builder.create(); 
     alertDialog.show(); 


    } 


    private class DeleteClass extends AsyncTask<Void, Void, DBhandler> { 
     @Override 
     protected DBhandler doInBackground(Void... params) { 
      dBhandler.delete(); 
      display(); 
      return dBhandler; 


     } 

    } 

    private class DeleteItemClass extends AsyncTask<Cursor, Void, DBhandler> { 
     @Override 
     protected DBhandler doInBackground(Cursor... params) { 
      dBhandler.deleteItem(Integer.parseInt(c.getString(0))); 
      display(); 
      return dBhandler; 
     } 
    } 
} 

そしてNote.java:

public class EditNote extends AppCompatActivity { 
    Button btn; 
    Button clear; 
    EditText editNote; 
    EditText editTextTitle; 
    DBhandler dBhandler; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.notecreate); 
     editNote = (EditText) findViewById(R.id.note); 
     btn = (Button) findViewById(R.id.save); 
     clear = (Button) findViewById(R.id.clear); 
     editTextTitle = (EditText) findViewById(R.id.title); 
     Bundle bundle = getIntent().getExtras(); 
     String newTitle = bundle.getString("newtitle"); 
     String newdata = bundle.getString("newdata"); 
     final int id = bundle.getInt("CursorId"); 
     dBhandler = new DBhandler(this); 
     editTextTitle.setText(newTitle); 
     editNote.setText(newdata); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String title = editTextTitle.getText().toString(); 
       String noteContent = editNote.getText().toString(); 
       // Log.d("IDNote",String .valueOf(id)); 
       Note note = new Note(id, noteContent, title); 
       note.setId(id); 
       note.setTitle(title); 
       note.setData(noteContent); 

        dBhandler.update(id,noteContent,title); 
         // new ListNotesActivity().display(); 
        Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show(); 

      } 
     }); 
     clear.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new SaveNoteActivity().clear(editNote, editTextTitle); 
      } 
     }); 
    } 

} 

そして、ここではListNotesActivityClassある

public class Note { 
    String data; 
    int id; 
    String title; 
    Note(){} 
    Note(String title){ 
     this.title=title; 
    } 
    Note(String data,String title) 
    { 
     this.title=title; 
     this.data=data; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    Note(int id,String data,String title){ 
     this.data=data; 
     this.id=id; 
     this.title=title; 

    } 

    public String getData() { 
     return data; 
    } 

    public void setData(String data) { 
     this.data = data; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
} 
+0

あなたは完全なコードを投稿することができます。助けが簡単になります –

+0

はい、私はコードを更新しました。 –

+0

EditNoteクラスのdisplayメソッドを呼び出すと、android.view.Window.Callbackエラーが発生します。それが役立つかもしれません。 –

答えて

1

実際に問題があなたのKEY_IDであり、あなたがいないそこのidのthatsを更新していますあなたのデータベース

あなたのリストビューをリフレッシュしないのはなぜですか?

ちょうどあなたのデータベース内の何かにIDを変更し、それは動作します。

+0

さて、それは動作しません –

+0

どちらも動作しません –

0

何か変更が登録されたコンテンツプロバイダにコミットを取得するたびにそれが通知されますContentObserver

を使用することができます。など

// Implement the onChange(boolean) method to delegate the change notification to 
// the onChange(boolean, Uri) method to ensure correct operation on older versions 
// of the framework that did not have the onChange(boolean, Uri) method. 
@Override 
public void onChange(boolean selfChange) { 
    onChange(selfChange, null); 
} 

// Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument. 
@Override 
public void onChange(boolean selfChange, Uri uri) { 
    // Handle change. 
} 

...

selfChange - > boolean型:これは自己変更通知である場合はTrue。

uri - > Uri:変更されたコンテンツのURIまたは不明の場合はnull。

ハッピーコーディングを試してみてください。

+0

私はこれについて考えていません。 –

+2

私はContentProvidersを使用していません。 –

関連する問題