android studio增删改查代码
时间: 2023-06-30 09:20:00 浏览: 224
Android通讯录的增删改查
以下是一个简单的Android Studio笔记应用程序的增删改查代码示例:
1. 创建一个Note类,包含笔记的标题和内容:
```
public class Note {
private int id;
private String title;
private String content;
public Note() {
}
public Note(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
2. 创建一个NoteDatabaseHelper类,用于管理SQLite数据库:
```
public class NoteDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "notes.db";
private static final int DATABASE_VERSION = 1;
public NoteDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE notes (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
```
3. 创建一个NoteAdapter类,用于显示笔记列表:
```
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
private List<Note> mNotes;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(Note note);
}
public NoteAdapter(List<Note> notes, OnItemClickListener listener) {
mNotes = notes;
mListener = listener;
}
@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
return new NoteViewHolder(itemView);
}
@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
final Note note = mNotes.get(position);
holder.titleTextView.setText(note.getTitle());
holder.contentTextView.setText(note.getContent());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(note);
}
});
}
@Override
public int getItemCount() {
return mNotes.size();
}
public static class NoteViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView contentTextView;
public NoteViewHolder(View itemView) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.title_text_view);
contentTextView = (TextView) itemView.findViewById(R.id.content_text_view);
}
}
}
```
4. 创建一个NoteActivity类,用于显示笔记列表和笔记详细信息:
```
public class NoteActivity extends AppCompatActivity {
private NoteDatabaseHelper mDbHelper;
private List<Note> mNotes;
private RecyclerView mRecyclerView;
private NoteAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
mDbHelper = new NoteDatabaseHelper(this);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mNotes = getAllNotes();
mAdapter = new NoteAdapter(mNotes, new NoteAdapter.OnItemClickListener() {
@Override
public void onItemClick(Note note) {
Intent intent = new Intent(NoteActivity.this, NoteDetailActivity.class);
intent.putExtra("note_id", note.getId());
startActivity(intent);
}
});
mRecyclerView.setAdapter(mAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NoteActivity.this, NoteDetailActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
mNotes = getAllNotes();
mAdapter.notifyDataSetChanged();
}
private List<Note> getAllNotes() {
List<Note> notes = new ArrayList<>();
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM notes", null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
Note note = new Note(id, title, content);
notes.add(note);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return notes;
}
}
```
5. 创建一个NoteDetailActivity类,用于编辑或添加笔记:
```
public class NoteDetailActivity extends AppCompatActivity {
private NoteDatabaseHelper mDbHelper;
private EditText mTitleEditText;
private EditText mContentEditText;
private int mNoteId = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
mDbHelper = new NoteDatabaseHelper(this);
mTitleEditText = (EditText) findViewById(R.id.title_edit_text);
mContentEditText = (EditText) findViewById(R.id.content_edit_text);
Intent intent = getIntent();
if (intent.hasExtra("note_id")) {
mNoteId = intent.getIntExtra("note_id", -1);
Note note = getNoteById(mNoteId);
mTitleEditText.setText(note.getTitle());
mContentEditText.setText(note.getContent());
}
}
public void onSaveButtonClick(View view) {
String title = mTitleEditText.getText().toString();
String content = mContentEditText.getText().toString();
if (title.isEmpty() || content.isEmpty()) {
Toast.makeText(this, "请填写标题和内容", Toast.LENGTH_SHORT).show();
return;
}
if (mNoteId == -1) {
addNoteToDatabase(title, content);
} else {
updateNoteInDatabase(mNoteId, title, content);
}
finish();
}
private Note getNoteById(int id) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM notes WHERE _id = ?", new String[]{String.valueOf(id)});
Note note = null;
if (cursor.moveToFirst()) {
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
note = new Note(id, title, content);
}
cursor.close();
db.close();
return note;
}
private void addNoteToDatabase(String title, String content) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
db.insert("notes", null, values);
db.close();
}
private void updateNoteInDatabase(int id, String title, String content) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
db.update("notes", values, "_id = ?", new String[]{String.valueOf(id)});
db.close();
}
}
```
希望这些示例代码对您有所帮助!
阅读全文