记账本
作者:互联网
创建一个empty activity
在activity_main.xml里编写主界面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="wrap_content" android:layout_height="650dp" /> <ImageButton android:id="@+id/add" android:onClick="addAccount" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="centerInside" android:layout_centerHorizontal="true" android:background="#00FF0000" android:src="@drawable/tianjia" android:layout_below="@+id/list_view" /> </RelativeLayout>View Code
然后写listview,创建xml文件并命名为list_item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/tv_title" android:layout_width="150dp" android:layout_height="80dp" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:gravity="center" android:singleLine="true" android:textSize="20sp" android:ellipsize="marquee" android:text="costTitle" /> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:textSize="20sp" android:layout_marginLeft="15dp" android:layout_toRightOf="@+id/tv_title" android:text="2020-05-31"/> <TextView android:id="@+id/tv_money" android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:textSize="25sp" android:textColor="#ffbd27" android:text="25"/> </RelativeLayout>View Code
写完listView之后要为其添加适配器,创建java文件并命名为ListAdapter
package com.example.myaccountapp; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class ListAdapter extends BaseAdapter { List<costList> mList; public ListAdapter(List<costList>list) { mList=list; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=mLayoutInflater.inflate(R.layout.list_item,null); //取出数据赋值 costList item=mList.get(position); TextView tv_title=view.findViewById(R.id.tv_title); TextView tv_date=view.findViewById(R.id.tv_date); TextView tv_money=view.findViewById(R.id.tv_money); //绑定 tv_title.setText(mList.get(position).getTitle()); tv_date.setText(mList.get(position).getDate()); tv_money.setText(mList.get(position).getMoney()); return view; } private List<costList>getmList; private LayoutInflater mLayoutInflater; public ListAdapter(Context context,List<costList>list) { mList=list; //通过外部传来的Context初始化LayoutInflater对象 mLayoutInflater=LayoutInflater.from(context); } }View Code
标签:tv,mList,记账,position,import,public,view 来源: https://www.cnblogs.com/headl-voi/p/15877345.html