模仿应用市场上记账软件实现记账软件的基本功能,并将记账的结果保存到Android本地数据库,并且功能 中至少需要包含recycleview控件显示所有的记账条目Android Studio代码
时间: 2024-05-06 14:17:42 浏览: 118
以下是一个简单的记账软件的Android Studio代码示例,实现了基本的记账功能并将结果保存到本地数据库中:
1. 首先在布局文件中添加一个EditText和一个Button,用于输入金额和提交记账条目:
```
<EditText
android:id="@+id/editTextAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="输入金额" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
```
2. 在Activity中获取EditText和Button的引用,并设置点击事件:
```
public class MainActivity extends AppCompatActivity {
private EditText editTextAmount;
private Button buttonAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAmount = findViewById(R.id.editTextAmount);
buttonAdd = findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 将记账条目添加到数据库中
addEntryToDatabase();
// 清空输入框
editTextAmount.setText("");
}
});
}
private void addEntryToDatabase() {
// 获取当前时间
Date now = new Date();
// 获取输入的金额
double amount = Double.parseDouble(editTextAmount.getText().toString());
// 创建一个新的记账条目
Entry entry = new Entry(amount, now);
// 将记账条目添加到数据库中
DatabaseHelper.getInstance(this).addEntry(entry);
}
}
```
3. 创建一个Entry类,用于表示一个记账条目:
```
public class Entry {
private double amount;
private Date date;
public Entry(double amount, Date date) {
this.amount = amount;
this.date = date;
}
public double getAmount() {
return amount;
}
public Date getDate() {
return date;
}
}
```
4. 创建一个DatabaseHelper类,用于管理数据库的创建和更新以及数据的插入和查询:
```
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "entries.db";
private static final int DATABASE_VERSION = 1;
private static DatabaseHelper instance;
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized DatabaseHelper getInstance(Context context) {
if (instance == null) {
instance = new DatabaseHelper(context.getApplicationContext());
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE entries (_id INTEGER PRIMARY KEY AUTOINCREMENT, amount REAL, date INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 数据库升级时的操作
}
public void addEntry(Entry entry) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("amount", entry.getAmount());
values.put("date", entry.getDate().getTime());
db.insert("entries", null, values);
db.close();
}
public List<Entry> getAllEntries() {
List<Entry> entries = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM entries ORDER BY date DESC", null);
while (cursor.moveToNext()) {
double amount = cursor.getDouble(cursor.getColumnIndex("amount"));
long dateInMillis = cursor.getLong(cursor.getColumnIndex("date"));
Date date = new Date(dateInMillis);
Entry entry = new Entry(amount, date);
entries.add(entry);
}
cursor.close();
db.close();
return entries;
}
}
```
5. 在布局文件中添加一个RecyclerView控件,用于显示所有的记账条目:
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEntries"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
6. 在Activity中获取RecyclerView的引用,并且将Adapter和LayoutManager设置给RecyclerView:
```
public class MainActivity extends AppCompatActivity {
private EditText editTextAmount;
private Button buttonAdd;
private RecyclerView recyclerViewEntries;
private EntriesAdapter entriesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAmount = findViewById(R.id.editTextAmount);
buttonAdd = findViewById(R.id.buttonAdd);
recyclerViewEntries = findViewById(R.id.recyclerViewEntries);
// 创建一个EntriesAdapter,并将其设置给RecyclerView
entriesAdapter = new EntriesAdapter(DatabaseHelper.getInstance(this).getAllEntries());
recyclerViewEntries.setAdapter(entriesAdapter);
// 设置LinearLayoutManager给RecyclerView
recyclerViewEntries.setLayoutManager(new LinearLayoutManager(this));
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 将记账条目添加到数据库中
addEntryToDatabase();
// 清空输入框
editTextAmount.setText("");
// 更新RecyclerView的数据
entriesAdapter.updateEntries(DatabaseHelper.getInstance(MainActivity.this).getAllEntries());
}
});
}
private void addEntryToDatabase() {
// 获取当前时间
Date now = new Date();
// 获取输入的金额
double amount = Double.parseDouble(editTextAmount.getText().toString());
// 创建一个新的记账条目
Entry entry = new Entry(amount, now);
// 将记账条目添加到数据库中
DatabaseHelper.getInstance(this).addEntry(entry);
}
}
```
7. 创建一个EntriesAdapter类,用于将数据绑定到RecyclerView的ViewHolder上:
```
public class EntriesAdapter extends RecyclerView.Adapter<EntriesAdapter.EntryViewHolder> {
private List<Entry> entries;
public EntriesAdapter(List<Entry> entries) {
this.entries = entries;
}
@NonNull
@Override
public EntryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_entry, parent, false);
return new EntryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull EntryViewHolder holder, int position) {
Entry entry = entries.get(position);
holder.textViewAmount.setText(String.format(Locale.getDefault(), "%.2f", entry.getAmount()));
holder.textViewDate.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(entry.getDate()));
}
@Override
public int getItemCount() {
return entries.size();
}
public void updateEntries(List<Entry> entries) {
this.entries = entries;
notifyDataSetChanged();
}
static class EntryViewHolder extends RecyclerView.ViewHolder {
TextView textViewAmount;
TextView textViewDate;
EntryViewHolder(@NonNull View itemView) {
super(itemView);
textViewAmount = itemView.findViewById(R.id.textViewAmount);
textViewDate = itemView.findViewById(R.id.textViewDate);
}
}
}
```
8. 在布局文件中添加一个list_item_entry.xml文件,用于表示每个记账条目的布局:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/textViewAmount"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textViewDate"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
```
这样,我们就完成了一个简单的记账软件的开发,并且将数据保存到了本地数据库中,并且使用RecyclerView控件显示所有的记账条目。
阅读全文