我目前正在做一個記事本應用程序。
當我按下一個按鈕添加一個新的筆記,它會帶我到筆記添加活動。
但是,當我點擊完成時,註釋本身不會更新到主頁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();
}
}
UPDATE
它給了我一個IndexOutOfBounds例外,在這條線 db.deleteNote(notesList.get(i));
在MainActivity.java在setOnItemLongClickListener()