用android studio实现简单的日记本详细代码,可增加日记到Dairy数据库中并且能用查看所有记录显示在listview中
时间: 2023-12-10 14:38:34 浏览: 151
Android Studio实现一个记账本,功能丰富,详细注释,优秀设计!
5星 · 资源好评率100%
以下是一个简单的日记本应用的代码,使用Android Studio和SQLite数据库来实现:
activity_main.xml布局文件:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入你的日记"
android:gravity="top"
android:padding="10dp"/>
<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
Dairy.java数据模型类:
```
public class Dairy {
private int id;
private String text;
private String date;
public Dairy() {}
public Dairy(int id, String text, String date) {
this.id = id;
this.text = text;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
```
DairyAdapter.java适配器类:
```
public class DairyAdapter extends BaseAdapter {
private Context context;
private List<Dairy> dairyList;
public DairyAdapter(Context context, List<Dairy> dairyList) {
this.context = context;
this.dairyList = dairyList;
}
@Override
public int getCount() {
return dairyList.size();
}
@Override
public Object getItem(int position) {
return dairyList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.item_dairy, null);
TextView textViewText = view.findViewById(R.id.textViewText);
TextView textViewDate = view.findViewById(R.id.textViewDate);
Dairy dairy = dairyList.get(position);
textViewText.setText(dairy.getText());
textViewDate.setText(dairy.getDate());
return view;
}
}
```
item_dairy.xml布局文件:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/textViewText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/textViewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#666666"/>
</LinearLayout>
```
MainActivity.java主活动类:
```
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button buttonSave;
private ListView listView;
private List<Dairy> dairyList = new ArrayList<>();
private DairyAdapter dairyAdapter;
private SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
buttonSave = findViewById(R.id.buttonSave);
listView = findViewById(R.id.listView);
dairyAdapter = new DairyAdapter(this, dairyList);
listView.setAdapter(dairyAdapter);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
if (!TextUtils.isEmpty(text)) {
insertDairy(text);
editText.setText("");
queryAllDairy();
}
}
});
sqLiteDatabase = openOrCreateDatabase("Dairy.db", Context.MODE_PRIVATE, null);
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS Dairy (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, date TEXT)");
queryAllDairy();
}
private void insertDairy(String text) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
sqLiteDatabase.execSQL("INSERT INTO Dairy (text, date) VALUES (?, ?)", new Object[]{text, date});
}
private void queryAllDairy() {
dairyList.clear();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM Dairy ORDER BY date DESC", null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String text = cursor.getString(cursor.getColumnIndex("text"));
String date = cursor.getString(cursor.getColumnIndex("date"));
Dairy dairy = new Dairy(id, text, date);
dairyList.add(dairy);
}
dairyAdapter.notifyDataSetChanged();
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
sqLiteDatabase.close();
}
}
```
以上代码实现了一个简单的日记本应用,主要通过SQLite数据库存储日记信息,并使用ListView显示所有的日记记录。代码中包含了数据模型类、适配器类、布局文件和主活动类等。其中主要的实现逻辑是:在点击“保存”按钮时将日记内容插入到数据库中,然后重新查询出所有的日记记录并更新ListView显示。
阅读全文