寒假学习15
Posted vvxvv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寒假学习15相关的知识,希望对你有一定的参考价值。
“记帐本”开始
MainActivity.java
package com.example.myapplicationsecond; 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 java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> mCostBeanList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); mCostBeanList = new ArrayList<>(); ListView costList = findViewById(R.id.lv_main); initCostDate(); costList.setAdapter(new CostListAdapter(this,mCostBeanList)); 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 initCostDate() { for (int i = 0; i < 6; i++) { CostBean costBean = new CostBean(); costBean.costTitle = "mock"; costBean.costDate = "11-11"; costBean.costMoney = "20"; mCostBeanList.add(costBean); } } @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); } }
CostBean.java
package com.example.myapplicationsecond; public class CostBean { public String costTitle; public String costDate; public String costMoney; }
CostListAdapter.java
package com.example.myapplicationsecond; 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 CostListAdapter extends BaseAdapter { private List<CostBean> mList; private Context mContext; private LayoutInflater mLayoutInflater; public CostListAdapter(Context context,List<CostBean> list){ mContext = context; mList = list; mLayoutInflater = 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 = mLayoutInflater.inflate(R.layout.list_item,null); viewHolder.mTvCostTitle = convertView.findViewById(R.id.tv_title); viewHolder.mTvCostDate = convertView.findViewById(R.id.tv_date); viewHolder.mTvCostMoney = convertView.findViewById(R.id.tv_cost); convertView.setTag(viewHolder); } else{ viewHolder = (ViewHolder) convertView.getTag(); } CostBean bean = mList.get(position); viewHolder.mTvCostTitle.setText(bean.costTitle); viewHolder.mTvCostDate.setText(bean.costDate); viewHolder.mTvCostMoney.setText(bean.costMoney); return convertView; } private static class ViewHolder{ public TextView mTvCostTitle; public TextView mTvCostDate; public TextView mTvCostMoney; } }
list_item.xml
<?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:id="@+id/tv_title" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginLeft="10dp" android:layout_alignParentLeft="true" android:gravity="center" android:singleLine="true" android:textSize="35sp" android:ellipsize="marquee" android:text="costTitle"/> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="80dp" android:textSize="20sp" android:gravity="center" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/tv_title" android:text="costDate"/> <TextView android:id="@+id/tv_cost" android:layout_width="wrap_content" android:layout_height="80dp" android:gravity="center" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:text="30" android:textSize="30sp"/> </RelativeLayout>
以上是关于寒假学习15的主要内容,如果未能解决你的问题,请参考以下文章