家庭記帳本安卓版開發:第四天

1、效果展現java

 

 帳戶名爲張志偉,登陸android

 

 查看帳目sql

 

 

 這是數據庫的數據數據庫

 

 

 

 2、總結app

學習了BaseAdapter適配器來根據查詢到的數據條數顯示錶單的行數,實現代碼和佈局的聯動。ide

3、部分原碼佈局

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.javathis

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.javaspa

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>
相關文章
相關標籤/搜索