家庭记账本安卓版开发:第四天

一、效果展示

家庭记账本安卓版开发:第四天

 

 账户名为张志伟,登录

家庭记账本安卓版开发:第四天

 

 查看账目

 家庭记账本安卓版开发:第四天

 

 这是数据库的数据

家庭记账本安卓版开发:第四天

 

 家庭记账本安卓版开发:第四天

 

 二、总结

学习了BaseAdapter适配器来根据查询到的数据条数显示表单的行数,实现代码和布局的联动。

三、部分原码

QueryAllActivity.java

package com.example.familybook;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.familybook.dao.BillDaoImpl;
import com.example.familybook.dao.IBillDao;
import com.example.familybook.entity.Bill;
import com.example.familybook.utils.ShowBillsAdapter;
import com.example.familybook.utils.ViewHolder;

import java.util.List;

public class QueryAllActivity extends Activity {
    private String mUsername;
    private String TAG="QueryAllActivity";
    private List <Bill> mShowBills;
    private List <ViewHolder> mViewHolder;
    private IBillDao mIBillDao;
    private SQLiteOpenHelper mBillDatabaseHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        this.setTitle("总览账目信息");

        //获取登录者的用户名
        Intent intent =getIntent();
        mUsername =intent.getStringExtra("username");
        Log.e(TAG,"使用者:"+mUsername);

        //初始化控件
        initView();

        //获取表格布局
        ListView ShowBillList=(ListView)findViewById(R.id.list_View);

        //获取从数据库获得的表单
        mShowBills=mIBillDao.listAllBill(mUsername);

        //新建并配置Show
        ShowBillsAdapter  ShowAdapter=new ShowBillsAdapter(mShowBills,this);
        ShowBillList.setAdapter(ShowAdapter);
        //添加事件监听
        initListener();
    }




    private void initListener() {


    }

    private void initView() {
        mIBillDao =new BillDaoImpl(QueryAllActivity.this);

    }
}

ShowBillsAdapter.java

package com.example.familybook.utils;
/**
 * 这是查看总览账目的列表帮助类
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.familybook.R;
import com.example.familybook.entity.Bill;

import java.util.List;

public class ShowBillsAdapter extends BaseAdapter {
    private List<Bill> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public ShowBillsAdapter(List<Bill> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 返回适配器中数据集中的个数
     * @return
     */
    @Override
    public int getCount() {
        return mList.size();
    }

    /**
     * 获取数据数据集中与指定索引对应的数据项
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    /**
     * 获取指定行对应的ID
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            /**
             * 将表单的标题与布局中的控件对应起来
             */
            viewHolder=new ViewHolder();
            convertView=mLayoutInflater.inflate(R.layout.activity_queryallbill,null);
            viewHolder.mBillType=(TextView)convertView.findViewById(R.id.show_bill_type);
            viewHolder.mBillMoney=(TextView)convertView.findViewById(R.id.show_bill_money);
            viewHolder.mBillDate=(TextView)convertView.findViewById(R.id.show_bill_date);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)convertView.getTag();
        }

        //获取一个bill实体类,将bill的数据封装到表格的内容中

        Bill bill=mList.get(position);
        //这里获取数据返回给布局
        viewHolder.mBillType.setText(bill.getType());
        viewHolder.mBillMoney.setText(bill.getMoney());
        viewHolder.mBillDate.setText(bill.getDate());
        return convertView;
    }
}

ViewHolder.java

package com.example.familybook.utils;
/**
 * 这是一个表单标题类
 */

import android.widget.TextView;

public class ViewHolder {
    public TextView mBillType;
    public TextView mBillMoney;
    public TextView mBillDate;
}

activity_list.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:background="@mipmap/queryallbill"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_View"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

activity_queryallbill.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:id="@+id/query_all_bill"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:orientation="vertical"
        tools:context=".QueryAllActivity" >
        <TextView
            android:id="@+id/show_bill_type"
            android:text="账目类型"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/show_bill_money"
            android:layout_width="wrap_content"
            android:text="账目金额"
            android:textSize="30dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/show_bill_type"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/show_bill_date"
            android:layout_width="wrap_content"
            android:text="账目日期"
            android:textSize="30dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/show_bill_money"
            android:layout_height="wrap_content"/>
    </RelativeLayout>


</ScrollView>

 

家庭记账本安卓版开发:第四天

上一篇:家庭记账小账本Android studio


下一篇:Leetcode | Trapping Rain Water