2016-08-06 60 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

它给了我一个IndexOutOfBounds例外,在这条线 db.deleteNote(notesList.get(i));在MainActivity.java在setOnItemLongClickListener()

回答

0

你得到的指数因为您从列表中删除了笔记,因此使用相同的索引从笔记中获取笔记名单

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

至于标题不显示,你需要检查你的数据库,看看是否存储在字符串,然后验证您的标题设置为注意级,然后getter方法返回一个字符串,最后Adapter将该字符串加载到正确的视图中。

如果您想做的稍微有点不同,请使用CursorAdapter而不是ArrayAdapter。这样,您可以在更新数据库时将光标重新加载到适配器,而不是尝试同步这两者。当你正在做db.addNotedb.deleteNote

0

适配器notesList没有改变。此方法更改数据库中的数据,但不更改为适配器列表中的数据,它是notesList = db.getAllNotes();

做的正确你必须在适配器的数据库更改列表中更改后。例如添加方法适配器,如:

public void addToList(Note note){ 

    list.add(note); 
    notifyDataSetChanged(); 

} 

并添加到数据库后使用它:

Not note=new Note(title.getText().toString(), message.getText().toString()); 
db.addNote(note); 
adapter.addToList(note); 
相关问题