Android开发之记账本开发第二天
一、说在前面
昨天完成了对BaseAdapter的学习,今天主要完成前期的准备和计划,并且完成了适配器的配置和测试。今天完成的内容较少,晚上早点睡明天完成数据库的部署和对业务逻辑的初步整理。
二、今天完成的源程序代码
package com.example.daliy; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> costBeanList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); costBeanList=new ArrayList<>(); ListView costList=findViewById(R.id.lv_main); initCostData(); costList.setAdapter(new CostAdapter(this,costBeanList)); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } private void initCostData() { for(int i=0;i<6;i++) { CostBean cb=new CostBean(); cb.costDate="12-12"; cb.costMoney="50"; cb.costTitle="heih"; costBeanList.add(cb); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:layout_width="100dp" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:text="CostTitlesdfsdf" android:textSize="35sp" android:gravity="center" android:ellipsize="marquee" android:singleLine="true" android:id="@+id/tv_costTitle" android:layout_height="80dp"/> <TextView android:layout_width="wrap_content" android:layout_height="80dp" android:layout_toRightOf="@id/tv_costTitle" android:textSize="30dp" android:gravity="center" android:id="@+id/tv_costDate" android:layout_marginLeft="15dp" android:text="costDate"/> <TextView android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:textSize="30sp" android:layout_marginRight="20dp" android:layout_alignParentRight="true" android:id="@+id/tv_costMoney" android:text="30"/> </RelativeLayout>
package com.example.daliy; 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 java.util.List; public class CostAdapter extends BaseAdapter { private List<CostBean> mlist; private LayoutInflater minflater; private Context mcontext; public CostAdapter(Context context, List<CostBean> list) { mlist = list; mcontext = context; minflater = LayoutInflater.from(context); } @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) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = minflater.inflate(R.layout.list_item, null); viewHolder.mTvcostTitle = convertView.findViewById(R.id.tv_costTitle); viewHolder.mTvcostMoney = convertView.findViewById(R.id.tv_costMoney); viewHolder.mTvcostDate = convertView.findViewById(R.id.tv_costDate); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } CostBean bean = mlist.get(position); viewHolder.mTvcostTitle.setText(bean.costTitle); viewHolder.mTvcostMoney.setText(bean.costMoney); viewHolder.mTvcostDate.setText(bean.costDate); return convertView; } private static class ViewHolder { public TextView mTvcostTitle; public TextView mTvcostDate; public TextView mTvcostMoney; } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
三、效果
item封装的效果,用来显示花费的备注,日期和钱