模仿应用市场上记账软件实现记账软件的基本功能,并将记账的结果保存到Android本地数据库,并且功能中至少需要包含recycleview控件显示所有的记账条目代码
时间: 2024-06-08 08:07:05 浏览: 135
以下是一个简单的记账软件的实现示例,它包含基本的记账功能和使用RecyclerView控件来显示所有的记账条目。该示例使用了Android本地数据库来保存记账信息。
首先,在您的项目中创建一个名为 "Account" 的 Java类,用于表示每个记账条目的数据:
```java
public class Account {
private String title;
private String amount;
private String date;
public Account(String title, String amount, String date) {
this.title = title;
this.amount = amount;
this.date = date;
}
public String getTitle() {
return title;
}
public String getAmount() {
return amount;
}
public String getDate() {
return date;
}
}
```
接下来,创建一个名为 "AccountAdapter" 的 RecyclerView 适配器类,用于将记账信息显示在 RecyclerView 中:
```java
public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.ViewHolder> {
private List<Account> accountList;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView amountTextView;
public TextView dateTextView;
public ViewHolder(View view) {
super(view);
titleTextView = view.findViewById(R.id.titleTextView);
amountTextView = view.findViewById(R.id.amountTextView);
dateTextView = view.findViewById(R.id.dateTextView);
}
}
public AccountAdapter(List<Account> accountList) {
this.accountList = accountList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.account_list_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Account account = accountList.get(position);
holder.titleTextView.setText(account.getTitle());
holder.amountTextView.setText(account.getAmount());
holder.dateTextView.setText(account.getDate());
}
@Override
public int getItemCount() {
return accountList.size();
}
}
```
接下来,创建一个名为 "MainActivity" 的活动,其中包含一个 RecyclerView 控件和一些用于添加记账条目的 UI 元素:
```java
public class MainActivity extends AppCompatActivity {
private List<Account> accountList = new ArrayList<>();
private AccountAdapter accountAdapter;
private EditText titleEditText;
private EditText amountEditText;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
accountAdapter = new AccountAdapter(accountList);
recyclerView.setAdapter(accountAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
titleEditText = findViewById(R.id.titleEditText);
amountEditText = findViewById(R.id.amountEditText);
addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addAccount();
}
});
}
private void addAccount() {
String title = titleEditText.getText().toString().trim();
String amount = amountEditText.getText().toString().trim();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(amount)) {
Account account = new Account(title, amount, date);
accountList.add(account);
accountAdapter.notifyDataSetChanged();
titleEditText.setText("");
amountEditText.setText("");
// 将账户信息保存到本地数据库中
saveAccount(account);
}
}
private void saveAccount(Account account) {
SQLiteDatabase db = new AccountDbHelper(this).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AccountContract.AccountEntry.COLUMN_NAME_TITLE, account.getTitle());
values.put(AccountContract.AccountEntry.COLUMN_NAME_AMOUNT, account.getAmount());
values.put(AccountContract.AccountEntry.COLUMN_NAME_DATE, account.getDate());
db.insert(AccountContract.AccountEntry.TABLE_NAME, null, values);
db.close();
}
}
```
最后,创建一个名为 "AccountDbHelper" 的帮助程序类,用于创建和管理本地数据库:
```java
public class AccountDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Account.db";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + AccountContract.AccountEntry.TABLE_NAME + " (" +
AccountContract.AccountEntry._ID + " INTEGER PRIMARY KEY," +
AccountContract.AccountEntry.COLUMN_NAME_TITLE + " TEXT," +
AccountContract.AccountEntry.COLUMN_NAME_AMOUNT + " TEXT," +
AccountContract.AccountEntry.COLUMN_NAME_DATE + " TEXT)";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + AccountContract.AccountEntry.TABLE_NAME;
public AccountDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
```
以上是一个简单的记账软件的实现示例,它包含基本的记账功能和使用 RecyclerView 控件来显示所有的记账条目。
阅读全文