Android家庭记账本开发第七天:查找功能的设计

在完成了之前添加功能的设计之后,查找功能就不在话下了,我们在activity_main.xml布局当中设置了一个查找的按钮并绑定了onClick函数为searchAccount,我们在MainActivity当中设置了一个用于跳转的方法,现在我们来看看查找方法的实现:

 1 package com.example.myapplication3;
 2 
 3 import android.annotation.SuppressLint;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.DatePicker;
 9 
10 import androidx.appcompat.app.AppCompatActivity;
11 
12 public class search_cost extends AppCompatActivity {
13     private DatePicker datePicker;
14 
15     @SuppressLint("MissingInflatedId")
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_search_cost);
20 
21         datePicker = findViewById(R.id.datePicker);
22 
23         // 设置确定按钮的点击事件监听器
24         Button btnConfirm = findViewById(R.id.button);
25 
26         btnConfirm.setOnClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 // 获取选择的年、月、日
30                 int year = datePicker.getYear();
31                 int month = datePicker.getMonth() + 1; // 月份从0开始,需要加1
32                 int day = datePicker.getDayOfMonth();
33 
34                 // 构造选择的日期字符串
35                 String selectedDate = year + "-" + month + "-" + day;
36 
37                 // 创建一个带有选择的日期数据的 Intent
38                 Intent resultIntent = new Intent();
39                 resultIntent.putExtra("selected_date", selectedDate);
40 
41                 // 将选择的日期数据返回给 MainActivity
42                 setResult(2, resultIntent);
43 
44                 // 结束当前活动
45                 finish();
46             }
47         });
48     }
49     public void goback(View view){
50         finish();
51     }
52 }

只是简单的查找日期的话,查找操作是比较简单的,这里也给出activity_search_cost.xml布局文件的代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:gravity="center">
 8 
 9     <DatePicker
10         android:id="@+id/datePicker"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_margin="8dp"
14         android:datePickerMode="spinner"
15         android:calendarViewShown="false"
16         />
17 
18     <Button
19         android:id="@+id/button"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:layout_marginLeft="40dp"
23         android:layout_marginRight="40dp"
24         android:background="#00BCD4"
25         android:text="确认"
26         android:textColor="#333333"
27         android:textSize="20dp" />
28 
29     <Button
30         android:onClick="goback"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:layout_marginLeft="40dp"
34         android:layout_marginRight="40dp"
35         android:layout_marginTop="20dp"
36         android:background="#00BCD4"
37         android:text="返回"
38         android:textColor="#333333"
39         android:textSize="20dp" />
40 </LinearLayout>

只是通过之前所讲到的日期选择器进行数据日期的查找,关于逻辑代码在有了之前的讲解之后应该没什么太难的地方,这里我们注意一下setResult方法,我的参数为2和数据,这里的2还可以是RESULT_OK这样的常量,其值是-1,RESULT_CANCELED:用于表示操作被取消,通常对应的值为0RESULT_FIRST_USER:作为用户定义的起始结果代码的常量值,通常大于1。为了使代码更加可读还可以自定义常量。

 1 @Override
 2     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
 3         super.onActivityResult(requestCode, resultCode, data);
 4         if(requestCode==1&&resultCode==1)
 5         {
 6             this.initData();
 7         }
 8         if(requestCode==2&&resultCode==2){
 9             // 获取选择的日期数据
10             String selectedDate = null;
11             if (data != null) {
12                 selectedDate = data.getStringExtra("selected_date");
13                 // 根据选择的日期从数据库中检索相关账单数据并显示在主界面上
14                 showBillsForSelectedDate(selectedDate);
15             }
16         }
17     }
18 
19     private void showBillsForSelectedDate(String selectedDate) {
20         // 根据选择的日期从数据库中检索相关账单数据
21         List<costList> billList = helper.getBillsForDate(selectedDate); // 假设 DBHelper 中有一个方法可以根据日期检索相关账单数据
22 
23         // 使用适配器将账单数据绑定到 ListView 上
24         ListAdapter adapter = new ListAdapter(this, billList);
25         listView.setAdapter(adapter);
26     }

这里是MainActivity当中处理查找的函数

 1 @SuppressLint("Range")
 2     public List<costList> getBillsForDate(String selectedDate) {
 3         List<costList> billList = new ArrayList<>();
 4         SQLiteDatabase db = this.getReadableDatabase();
 5 
 6         // 查询 account 表中日期为 selectedDate 的账单数据
 7         String[] projection = {"_id", "Title", "Date", "Money"};
 8         String selection = "Date = ?";
 9         String[] selectionArgs = {selectedDate};
10         Cursor cursor = db.query("account", projection, selection, selectionArgs, null, null, null);
11 
12         // 遍历查询结果,并将每一行数据添加到 billList 中
13         if (cursor != null && cursor.moveToFirst()) {
14             do {
15                 costList bill = new costList();
16                 bill.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
17                 bill.setTitle(cursor.getString(cursor.getColumnIndex("Title")));
18                 bill.setDate(cursor.getString(cursor.getColumnIndex("Date")));
19                 bill.setMoney(cursor.getString(cursor.getColumnIndex("Money")));
20                 billList.add(bill);
21             } while (cursor.moveToNext());
22         }
23 
24         // 关闭 cursor 和数据库连接
25         if (cursor != null) {
26             cursor.close();
27         }
28         db.close();
29 
30         return billList;
31     }

这是数据库当中根据日期查找账单的函数,有了之前的介绍这些应该都算是比较简单的了。

posted @ 2024-02-22 15:54  起名字真难_qmz  阅读(53)  评论(0)    收藏  举报