码迷,mamicode.com
首页 > 其他好文 > 详细

寒假学习15

时间:2020-02-15 20:36:10      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:one   with   private   amp   pack   span   idm   googl   enter   

“记帐本”开始

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

标签:one   with   private   amp   pack   span   idm   googl   enter   

原文地址:https://www.cnblogs.com/vvxvv/p/12313446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!