2016-08-06 3 views
1

私は現在メモ帳アプリケーションを作成中です。
ボタンを押して新しいメモを追加すると、アクティビティを追加するメモに移動します。
しかし、終了をクリックすると、ノート自体はホームページListViewに更新されません。
ListViewにはアイテムの行がありますが、行のタイトルは表示されません。
何が問題なのですか?ListViewが最新のメモを追加して更新されません(メモアプリを作成)

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private Button button; 
private ListView listview; 
public static NoteAdapter adapter; 
private ArrayList<Note> notesList; 
private DatabaseHandler db; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    db = new DatabaseHandler(this); 

    listview = (ListView) findViewById(R.id.listView); 

    notesList = db.getAllNotes(); 
    adapter = new NoteAdapter(getApplicationContext(), notesList); 

    button = (Button) findViewById(R.id.addButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getApplicationContext(), NoteAddClass.class); 
      startActivity(intent); 
      adapter.notifyDataSetChanged(); 
     } 
    }); 

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Note note = db.getNote(i); 
      Intent intent = new Intent(getApplicationContext(), NoteViewClass.class); 
      intent.putExtra("title", note.getTitle()); 
      intent.putExtra("message", note.getContent()); 

      startActivity(intent); 
     } 
    }); 


    listview.setAdapter(adapter); 

    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 
      notesList.remove(i); 
      adapter.notifyDataSetChanged(); 
      db.deleteNote(notesList.get(i)); 
      return false; 
     } 
    }); 
} 
} 

NoteViewClass.java

public class NoteViewClass extends AppCompatActivity { 

private TextView title, message; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.note_view_layout); 

    title = (TextView) findViewById(R.id.titleViewTextView); 
    message = (TextView) findViewById(R.id.messageViewTextView); 

    Intent intent = getIntent(); 

    String title_intent = intent.getStringExtra("title"); 
    String message_intent = intent.getStringExtra("message"); 

    title.setText(title_intent); 
    message.setText(message_intent); 
} 
} 

NoteAddClass.java

public class NoteAddClass extends AppCompatActivity { 

private EditText title, message; 
private Button button; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.note_add_layout); 

    title = (EditText) findViewById(R.id.titleEditText); 
    message = (EditText) findViewById(R.id.messageEditText); 
    button = (Button) findViewById(R.id.finishButton); 

    DatabaseHandler db = new DatabaseHandler(this); 

    db.addNote(new Note(title.getText().toString(), message.getText().toString())); 

    MainActivity.adapter.notifyDataSetChanged(); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
      startActivity(intent); 
     } 
    }); 
} 
} 

NoteAdapter.java

public class NoteAdapter extends ArrayAdapter<Note> { 

public static class ViewHolder { 
    TextView titleTextView; 
} 

public NoteAdapter(Context context, ArrayList<Note> list) { 
    super(context, 0, list); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Note note = getItem(position); 

    ViewHolder viewHolder; 

    if(convertView == null) { 
     //save viewholder 
     viewHolder = new ViewHolder(); 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, parent, false); 
     viewHolder.titleTextView = (TextView) convertView.findViewById(R.id.titleRowTextView); 
     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    viewHolder.titleTextView.setText(note.getTitle()); 

    return convertView; 
} 
} 

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper { 

// All Static variables 
// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "notesManager"; 

// Contacts table name 
private static final String TABLE_OF_NOTES = "notes"; 

// Contacts Table Columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_TITLE = "title"; 
private static final String KEY_CONTENT = "content"; 

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

@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 
    String CREATE_DATABASE_TABLE = "CREATE TABLE " 
      + TABLE_OF_NOTES + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," 
      + KEY_TITLE + " TEXT," 
      + KEY_CONTENT + " TEXT" + ")"; 

    sqLiteDatabase.execSQL(CREATE_DATABASE_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_OF_NOTES); 

    // Create tables again 
    onCreate(sqLiteDatabase); 
} 

//!--------- Below to Handle CRUD Operations (Create, Read, Update, Delete)--------------! 

public void addNote(Note note) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_TITLE, note.getTitle()); // Note Title 
    values.put(KEY_CONTENT, note.getContent()); // Note Message 

    // Inserting Row 
    db.insert(TABLE_OF_NOTES, null, values); 
    db.close(); // Closing database connection 
} 

//accepts a single id and returns the corresponding row item to this ID 
public Note getNote(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_OF_NOTES, new String[] 
      { KEY_ID, KEY_TITLE, KEY_CONTENT }, KEY_ID + "=?", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    //the first constructor, fetching the contents of the row 
    Note note = new Note(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2)); 
    // return contact 
    return note; 
} 

//returns all notes in an arraylist format 
public ArrayList<Note> getAllNotes() { 
    ArrayList<Note> noteArrayList = new ArrayList<Note>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_OF_NOTES; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Note note = new Note(); 
      note.setId(Integer.parseInt(cursor.getString(0))); 
      note.setTitle(cursor.getString(1)); 
      note.setContent(cursor.getString(2)); 

      // Adding contact to list 
      noteArrayList.add(note); 
     } while (cursor.moveToNext()); 
    } 

    // return contact list 
    return noteArrayList; 
} 

//returns the total number of notes in the database 
public int getNotesCount() { 
    String countQuery = "SELECT * FROM " + TABLE_OF_NOTES; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

//updates a single note object i the database 
public int updateNote(Note note) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_TITLE, note.getTitle()); 
    values.put(KEY_CONTENT, note.getContent()); 

    // updating row 
    return db.update(TABLE_OF_NOTES, values, KEY_ID + " = ?", 
      new String[] { String.valueOf(note.getId())}); 
} 

//deleting a note 
public void deleteNote(Note note) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_OF_NOTES, KEY_ID + " = ?", 
      new String[] { String.valueOf(note.getId()) }); 
    db.close(); 
} 
} 

enter image description here

UPDATE

は、それはあなたがインデックスを取得しているsetOnItemLongClickListener()

答えて

0

にMainActivity.javaにこの行 db.deleteNote(notesList.get(i));に私にIndexOutOfBounds例外を与えましたリストからメモを削除してから、その同じインデックスを使用してリスト表示されないタイトルについては

notesList.remove(i); // root problem 
adapter.notifyDataSetChanged(); 
db.deleteNote(notesList.get(i)); 

、あなたは注意クラスにタイトルを設定していることを確認した後、文字列が格納されているかどうかを確認するために、データベースをチェックする必要があり、その後、getterメソッドは、その文字列を返します。最後に、アダプタはその文字列を正しいビューにロードしています。

これを若干違う場合は、ArrayAdapterの代わりにCursorAdapterを使用してください。このようにして、データベースを更新するときに、2つを同期させるのではなく、アダプターにカーソルを再ロードすることができます。

0

あなたはdb.addNoteまたはdb.deleteNoteを行っているときに、アダプタnotesListが変更されていません。このメソッドはデータベース内のデータを変更しますが、アダプタリストではデータを変更しません。notesList = db.getAllNotes();

正しいことアダプターのDB変更リストの変更が必要です。

public void addToList(Note note){ 

    list.add(note); 
    notifyDataSetChanged(); 

} 

とDBに追加した後、それを使用します:

Not note=new Note(title.getText().toString(), message.getText().toString()); 
db.addNote(note); 
adapter.addToList(note); 
例えばのようにアダプタにメソッドを追加
関連する問題