用android studio做一个日记app的实现代码
时间: 2023-12-03 08:03:56 浏览: 113
基于Android Studio开发的安卓的记事本app
5星 · 资源好评率100%
以下是一个简单的日记App的实现代码,使用了SQLite数据库存储数据:
1. activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Content" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<ListView
android:id="@+id/diaryListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
2. Diary.java
```
public class Diary {
private int id;
private String title;
private String content;
private String date;
public Diary(int id, String title, String content, String date) {
this.id = id;
this.title = title;
this.content = content;
this.date = date;
}
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;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
```
3. DiaryAdapter.java
```
public class DiaryAdapter extends BaseAdapter {
private Context context;
private List<Diary> diaryList;
public DiaryAdapter(Context context, List<Diary> diaryList) {
this.context = context;
this.diaryList = diaryList;
}
@Override
public int getCount() {
return diaryList.size();
}
@Override
public Object getItem(int position) {
return diaryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.diary_item_layout, parent, false);
}
TextView titleTextView = convertView.findViewById(R.id.titleTextView);
TextView contentTextView = convertView.findViewById(R.id.contentTextView);
TextView dateTextView = convertView.findViewById(R.id.dateTextView);
Diary diary = diaryList.get(position);
titleTextView.setText(diary.getTitle());
contentTextView.setText(diary.getContent());
dateTextView.setText(diary.getDate());
return convertView;
}
}
```
4. MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private EditText titleEditText, contentEditText;
private Button saveButton;
private ListView diaryListView;
private List<Diary> diaryList;
private DiaryAdapter diaryAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);
saveButton = findViewById(R.id.saveButton);
diaryListView = findViewById(R.id.diaryListView);
diaryList = new ArrayList<>();
diaryAdapter = new DiaryAdapter(this, diaryList);
diaryListView.setAdapter(diaryAdapter);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString().trim();
String content = contentEditText.getText().toString().trim();
if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
Toast.makeText(MainActivity.this, "Title and content cannot be empty", Toast.LENGTH_SHORT).show();
return;
}
Diary diary = new Diary(0, title, content, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
DiaryDBHelper dbHelper = new DiaryDBHelper(MainActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", diary.getTitle());
values.put("content", diary.getContent());
values.put("date", diary.getDate());
db.insert("diary", null, values);
diaryList.add(diary);
diaryAdapter.notifyDataSetChanged();
titleEditText.setText("");
contentEditText.setText("");
}
});
DiaryDBHelper dbHelper = new DiaryDBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("diary", null, null, null, null, null, "id desc");
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"));
String date = cursor.getString(cursor.getColumnIndex("date"));
Diary diary = new Diary(id, title, content, date);
diaryList.add(diary);
} while (cursor.moveToNext());
}
cursor.close();
diaryAdapter.notifyDataSetChanged();
}
}
```
5. DiaryDBHelper.java
```
public class DiaryDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "diary.db";
private static final int DB_VERSION = 1;
public DiaryDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE diary (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, date TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS diary");
onCreate(db);
}
}
```
6. diary_item_layout.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="@+id/dateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
```
阅读全文