我目前正在制作记事本应用程序 当我按一个按钮添加一个新音符时,它会转到添加活动的音符 但是,当我点击完成时,注释本身不会更新到主页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();
}
}
更新
它在这一行上给了我一个IndexOutOfBounds异常
db.deleteNote(notesList.get(i));
setOnItemLongClickListener()
答案 0 :(得分:0)
由于您从列表中删除了注释,然后使用相同的索引从列表中获取注释,因此您获得的索引超出范围
{{1}}
对于未显示的标题,您需要检查数据库以查看字符串是否存储,然后验证您是否将标题设置为Note类,然后getter方法返回该字符串,最后是适配器正在将该字符串加载到正确的视图中。
如果您想稍微不同,请查看使用CursorAdapter而不是ArrayAdapter。这样,您可以在更新数据库时将光标重新加载到适配器,而不是尝试同步这两者。
答案 1 :(得分: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);