Android实现增删改查笔记本
时间: 2023-08-05 08:17:09 浏览: 129
以下是一个简单的 Android 笔记本应用的代码示例,包括增加、删除、修改和查找笔记的功能:
1. 在 activity_main.xml 中添加一个 ListView 元素和三个 Button 元素,以便用户列出、添加、编辑和删除笔记:
```xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/editButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"/>
<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"/>
```
2. 在 note_item.xml 中添加一个 TextView 元素,以便在 ListView 中显示笔记的标题:
```xml
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"/>
```
3. 在 MainActivity.java 文件中编写以下代码:
```java
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button addButton, editButton, deleteButton;
ArrayList<String> notes;
ArrayAdapter<String> adapter;
int selectedIndex = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
addButton = findViewById(R.id.addButton);
editButton = findViewById(R.id.editButton);
deleteButton = findViewById(R.id.deleteButton);
notes = new ArrayList<>();
adapter = new ArrayAdapter<>(this, R.layout.note_item, R.id.titleTextView, notes);
listView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNoteDialog("New note", "", true);
}
});
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedIndex != -1) {
String note = notes.get(selectedIndex);
showNoteDialog("Edit note", note, false);
} else {
Toast.makeText(MainActivity.this, "Please select a note to edit", Toast.LENGTH_SHORT).show();
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedIndex != -1) {
String note = notes.get(selectedIndex);
showDeleteDialog(note);
} else {
Toast.makeText(MainActivity.this, "Please select a note to delete", Toast.LENGTH_SHORT).show();
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectedIndex = i;
}
});
}
private void showNoteDialog(String title, String note, final boolean isNew) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
final EditText input = new EditText(this);
input.setText(note);
builder.setView(input);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String text = input.getText().toString();
if (isNew) {
notes.add(text);
} else {
notes.set(selectedIndex, text);
}
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
private void showDeleteDialog(final String note) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete note");
builder.setMessage("Are you sure you want to delete this note?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
notes.remove(note);
adapter.notifyDataSetChanged();
selectedIndex = -1;
}
});
builder.setNegativeButton("No", null);
builder.show();
}
}
```
4. 运行应用程序并测试添加、编辑、删除和查找笔记的功能。
注意:这只是一个简单的笔记本应用程序示例,未进行任何输入验证或错误处理。在实际应用程序中,您可能需要添加更多的代码来确保应用程序的稳定性和安全性。
阅读全文