美文网首页我爱编程
《小曼的账本》开发记录 【2.建立数据模型,搭建列表】

《小曼的账本》开发记录 【2.建立数据模型,搭建列表】

作者: 做梦枯岛醒 | 来源:发表于2018-04-14 21:56 被阅读52次

    上一篇我们搭建了基础UI,这一次我们建立一下数据模型。

    首先对于数据必须建立一个实体类,那么我们当前的实体类代码如下:

    package cn.surine.sm.Pojo;
    
    import org.litepal.crud.DataSupport;
    
    /**
     * Created by Surine on 2018/4/14.
     */
    
    //订单实体
    public class Order extends DataSupport {
       private long id;
       private String date;  //日期
       private String time;  //时间
       private String orderNumber;  //订单编号
       private String Buyer;  //买家名
       private String AmountOfRedEnvelopes;  //红包金额
       private String GeneralCommission;  //总佣金
       private String ActualCommission;  //实际佣金
       private String profit;  //利润
       private String commodity;  //商品名
       private String college;  //学校名
       private String isRefund;  //是否退款
       private String refundtatus;  //退款状态
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public String getDate() {
            return date;
        }
    
        public void setDate(String date) {
            this.date = date;
        }
    
        public String getTime() {
            return time;
        }
    
        public void setTime(String time) {
            this.time = time;
        }
    
        public String getOrderNumber() {
            return orderNumber;
        }
    
        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }
    
        public String getBuyer() {
            return Buyer;
        }
    
        public void setBuyer(String buyer) {
            Buyer = buyer;
        }
    
        public String getAmountOfRedEnvelopes() {
            return AmountOfRedEnvelopes;
        }
    
        public void setAmountOfRedEnvelopes(String amountOfRedEnvelopes) {
            AmountOfRedEnvelopes = amountOfRedEnvelopes;
        }
    
        public String getGeneralCommission() {
            return GeneralCommission;
        }
    
        public void setGeneralCommission(String generalCommission) {
            GeneralCommission = generalCommission;
        }
    
        public String getActualCommission() {
            return ActualCommission;
        }
    
        public void setActualCommission(String actualCommission) {
            ActualCommission = actualCommission;
        }
    
        public String getProfit() {
            return profit;
        }
    
        public void setProfit(String profit) {
            this.profit = profit;
        }
    
        public String getCommodity() {
            return commodity;
        }
    
        public void setCommodity(String commodity) {
            this.commodity = commodity;
        }
    
        public String getCollege() {
            return college;
        }
    
        public void setCollege(String college) {
            this.college = college;
        }
    
        public String getIsRefund() {
            return isRefund;
        }
    
        public void setIsRefund(String isRefund) {
            this.isRefund = isRefund;
        }
    
        public String getRefundtatus() {
            return refundtatus;
        }
    
        public void setRefundtatus(String refundtatus) {
            this.refundtatus = refundtatus;
        }
    
        public Order(long id, String date, String time, String orderNumber, String buyer, String amountOfRedEnvelopes, String generalCommission, String actualCommission, String profit, String commodity, String college, String isRefund, String refundtatus) {
            this.id = id;
            this.date = date;
            this.time = time;
            this.orderNumber = orderNumber;
            Buyer = buyer;
            AmountOfRedEnvelopes = amountOfRedEnvelopes;
            GeneralCommission = generalCommission;
            ActualCommission = actualCommission;
            this.profit = profit;
            this.commodity = commodity;
            this.college = college;
            this.isRefund = isRefund;
            this.refundtatus = refundtatus;
        }
    
        public Order() {
        }
    }
    

    这里继承了DataSupport,因为我们这里使用了Litepal这款框架。
    (LitePal全集:https://blog.csdn.net/u013277740/article/details/71173010

    Litepal第一步

    1.导入依赖

    compile 'org.litepal.android:core:1.3.0'
    

    2.构建litepal基本信息
    在src/main/assets/目录下建立litepal.xml文件描述litepal

    <?xml version="1.0" encoding="utf-8"?>
    <litepal>
        <!--数据库名--> 
        <dbname value="Order" ></dbname>
        <!--数据库版本-->
        <version value="1" ></version>
        <!--数据表,对应实体类-->
        <list>
            <mapping class="cn.surine.sm.Pojo.Order"></mapping>
        </list>
    </litepal>
    

    3.配置application
    增加一行
    android:name="org.litepal.LitePalApplication"

       <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:name="org.litepal.LitePalApplication"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" >
     …//省略的代码…
    

    4.最后一步,就是建立实体类啦,注意要继承自DataSupport,也就是上面的一大片代码。

    建立了Pojo实体和数据库的联系之后,进行增删改查的操作就很容易了。

    修改一下MainActivity.java
    实现数据库初始化

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
           //数据库初始化
            Connector.getDatabase();
            updateFragment(f1);
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        }
    

    2018年4月14日 20点23分

    刚读了一会英语,现在搭建list。
    我们使用的是BRVAH这款框架(参考:https://www.jianshu.com/p/b343fcff51b0)

    1.首先增加依赖
    先在 build.gradle(Project:XXXX) 的 repositories 添加:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
    

    然后在 build.gradle(Module:app) 的 dependencies 添加:

    dependencies {
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
    }
    

    2.建立列表的话我喜欢第一步先添加RecyclerView和Fab

    修改fragment_first.xml文件,添加RecyclerView

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <android.support.v7.widget.RecyclerView
          android:id="@+id/recycleview"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
      </android.support.v7.widget.RecyclerView>
    
      <android.support.design.widget.FloatingActionButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="true"
          android:layout_margin="16dp"
          android:id="@+id/add"
          android:src="@drawable/ic_edit_add_24dp"
          />
    </RelativeLayout>
    

    3.然后建立item.xml布局,设计布局去了……不要吵。
    十分钟后回来。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="5dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="13dp"
                android:layout_marginTop="12dp"
                android:text="4.13"
                android:textColor="@color/colorAccent"
                android:textSize="38sp" />
    
            <TextView
                android:id="@+id/orderName"
                android:layout_width="match_parent"
                android:ellipsize="marquee"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/data"
                android:layout_marginStart="21dp"
                android:layout_marginTop="5dp"
                android:layout_toEndOf="@+id/data"
                android:text="《第一行代码 第二版》等全套书籍"
                android:textColor="@android:color/background_dark"
                android:textSize="15sp"
                android:textStyle="bold"
                android:singleLine="true" />
    
            <TextView
                android:id="@+id/orderNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_alignBottom="@id/data"
                android:layout_alignStart="@+id/orderName"
                android:layout_below="@+id/orderName"
                android:text="112345678765432345" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lin1"
                android:layout_margin="15dp"
                android:layout_below="@id/orderNumber"
                >
    
                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignStart="@+id/data"
                    android:layout_below="@+id/orderNumber"
                    android:text="买家名:"
                    android:layout_weight="1"
                    android:textColor="@android:color/background_dark" />
                <TextView
                    android:id="@+id/name"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/textView6"
                    android:layout_toEndOf="@+id/textView6"
                    android:text="NAME" />
    
                <TextView
                    android:id="@+id/textView10"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/name"
                    android:layout_centerHorizontal="true"
                    android:text="学校:"
                    android:textColor="@android:color/background_dark" />
    
                <TextView
                    android:id="@+id/school"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/textView10"
                    android:layout_toEndOf="@+id/textView10"
                    android:layout_weight="1"
                    android:text="SCHOOL" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="利润:"
                    android:textColor="@android:color/background_dark" />
    
                <TextView
                    android:id="@+id/profit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="MON" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_below="@id/lin1"
                android:layout_margin="10dp"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView7"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="红包:"
                    android:textColor="@android:color/background_dark" />
    
                <TextView
                    android:id="@+id/redbag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/textView7"
                    android:layout_toEndOf="@+id/textView7"
                    android:layout_weight="1"
                    android:text="REDBAG" />
    
                <TextView
                    android:id="@+id/textView8"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/redbag"
                    android:layout_alignBottom="@+id/redbag"
                    android:text="总佣金:"
                    android:textColor="@android:color/background_dark" />
    
                <TextView
                    android:id="@+id/GeneralCommission"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/textView8"
                    android:layout_toEndOf="@+id/textView8"
                    android:text="ALL" />
    
                <TextView
                    android:id="@+id/textView14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/GeneralCommission"
                    android:layout_weight="1"
                    android:text="实际佣金:"
                    android:textColor="@android:color/background_dark" />
    
                <TextView
                    android:id="@+id/ActualCommission"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/textView14"
                    android:layout_toEndOf="@+id/textView14"
                    android:layout_weight="1"
                    android:text="ACT" />
            </LinearLayout>
    
            <Button
                android:id="@+id/isRefund"
                style="@style/Widget.AppCompat.Button.Borderless.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@+id/orderNumber"
                android:layout_alignParentBottom="true"
                android:text="退款" />
    
            <Button
                android:id="@+id/RefundStatus"
                style="@style/Widget.AppCompat.Button.Borderless.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/isRefund"
                android:layout_toStartOf="@+id/isRefund"
                android:text="退款状态" />
    
            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/data"
                android:layout_alignTop="@+id/RefundStatus"
                android:layout_marginTop="14dp"
                android:text="20点53分" />
    
    
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    
    

    这里是第一版布局,粗略的设计一下。
    4.实现Adapter

    package cn.surine.sm.Adapter;
    
    import com.chad.library.adapter.base.BaseQuickAdapter;
    import com.chad.library.adapter.base.BaseViewHolder;
    
    import java.util.List;
    
    import cn.surine.sm.Pojo.Orders;
    import cn.surine.sm.R;
    
    /**
     * Created by Surine on 2018/4/14.
     */
    
    public class OrderAdapter extends BaseQuickAdapter<Orders, BaseViewHolder> {
        public OrderAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }
    
        @Override
        protected void convert(BaseViewHolder helper, Orders item) {
           helper.setText(R.id.data,item.getDate());
           helper.setText(R.id.orderName,item.getCommodity());
           helper.setText(R.id.orderNumber,item.getOrderNumber());
           helper.setText(R.id.name,item.getBuyer());
           helper.setText(R.id.school,item.getCollege());
           helper.setText(R.id.profit,item.getProfit());
           helper.setText(R.id.redbag,item.getAmountOfRedEnvelopes());
           helper.setText(R.id.GeneralCommission,item.getGeneralCommission());
           helper.setText(R.id.ActualCommission,item.getActualCommission());
           helper.setText(R.id.time,item.getTime());
           helper.setText(R.id.RefundStatus,item.getRefundtatus());
        }
    }
    
    

    这个没啥好说的,框架里的设计方法,不清楚的可以仔细研究一下这个框架。

    5.使用,修改FirstFragment.java

        private OrderAdapter adapter;
        private List<Orders> data = new ArrayList<>();
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            //引用创建好的xml布局
            View view = inflater.inflate(R.layout.fragment_first, container, false);
            unbinder = ButterKnife.bind(this, view);
            recycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
           //初始化数据,可以先弄假数据来测试
            init();
          //配置适配器
            adapter = new OrderAdapter(R.layout.item_order,data);
            recycleview.setAdapter(adapter);
            return view;
    
        }
    

    实际效果图:

    《小曼的账本》开发记录 【2.建立数据模型,搭建列表】

    这里测试了一下,每次切换页面的时候数据都会接着加载一部分,这里不用担心,我们会在后面优化。

    好了,本篇文章就到这里啦。
    下一篇我们将从增加数据开始做起,同时优化一些细节。

    相关文章

      网友评论

        本文标题:《小曼的账本》开发记录 【2.建立数据模型,搭建列表】

        本文链接:https://www.haomeiwen.com/subject/nuglkftx.html