Android开发之记账本开发第五天

仰天大笑出门去,我辈岂是蓬蒿人。这篇文章主要讲述Android开发之记账本开发第五天相关的知识,希望能为你提供帮助。
一、说在前面 昨天对业务逻辑进行进一步的完善,遇到了大问题,今天在原有基础上添加了搜索和删除,并添加了一个数据的属性以下拉表的形式添加。遇到的问题,下拉表的信息接不到,已经解决。同时也非常感谢一篇文章的作者的分享,让我学会了下拉框的取值方法,在此附上链接:https://blog.csdn.net/S__zO/article/details/45502995 二、今天完成的源代码

Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
package com.example.daliy; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import android.widget.Spinner; import android.widget.Toast; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private static final String TAG="MainActivity"; private List< CostBean> costBeanList; private DatabaseHelper helper; private String selectText="oo"; CostAdapter mAdapter; @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< > (); helper=new DatabaseHelper(this); ListView costList=findViewById(R.id.lv_main); initCostData(); mAdapter = new CostAdapter(this, costBeanList); costList.setAdapter(mAdapter); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater=LayoutInflater.from(MainActivity.this); View viewDialog =inflater.inflate(R.layout.new_cost,null); final EditText cost_title=viewDialog.findViewById(R.id.et_cost_title); final EditText cost_money=viewDialog.findViewById(R.id.et_cost_money); final DatePicker cost_date=viewDialog.findViewById(R.id.dp_cost_date); final Spinner cost_type=viewDialog.findViewById(R.id.sp_type); cost_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView< ?> parent, View view, int position, long id) { selectText=cost_type.getSelectedItem().toString(); Log.d(TAG, "onItemSelected: +选择运行了"); }@Override public void onNothingSelected(AdapterView< ?> parent) {} }); builder.setTitle("新账目"); builder.setView(viewDialog); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(TextUtils.isEmpty(cost_title.getText())){ Toast.makeText(MainActivity.this,"备注为空",Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty(cost_money.getText())){ Toast.makeText(MainActivity.this,"金额为空",Toast.LENGTH_SHORT).show(); return; } CostBean costBean=new CostBean(); costBean.costType=selectText; Log.d(TAG, "onClick: "+selectText); costBean.costTitle=cost_title.getText().toString(); costBean.costMoney=cost_money.getText().toString(); costBean.costDate=cost_date.getYear()+"-"+(cost_date.getMonth()+1)+"-"+cost_date.getDayOfMonth(); helper.insertCost(costBean); costBeanList.add(costBean); mAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("Cancel",null); builder.create().show(); } }); }private void initCostData() { //helper.deleteAllCost(); //for(int i=0; i< 6; i++) { // //CostBean cb=new CostBean(); //cb.costDate="12-12"; //cb.costMoney="50"; //cb.costTitle=i+"heih"; //helper.insertCost(cb); //} Cursor cursor = helper.getAllCost(); if(cursor!=null){ while (cursor.moveToNext()){ CostBean costBean=new CostBean(); costBean.costType=cursor.getString(cursor.getColumnIndex("cost_type")); costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title")); costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date")); costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money")); costBeanList.add(costBean); } cursor.close(); } }@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) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_chart) {Intent intent = new Intent(MainActivity.this,ChartActivity.class); intent.putExtra("cost_list", (Serializable) costBeanList); startActivity(intent); return true; } else if(id==R.id.delete_allData){ helper.deleteAllCost(); costBeanList.clear(); mAdapter.notifyDataSetChanged(); return true; } else if(id==R.id.query){ Intent intent = new Intent(MainActivity.this,QueryActivity.class); startActivity(intent); } return super.onOptionsItemSelected(item); } }

MainActivity
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
package com.example.daliy; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; public class QueryActivity extends Activity { EditText queryKey; Button doResearch; DatabaseHelper helper; ListView listView; List< CostBean> list; //private String queryKey; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_query_layout); listView=findViewById(R.id.lv_query); queryKey = findViewById(R.id.et_queryKey); doResearch = findViewById(R.id.bt_do_research); helper=new DatabaseHelper(this); list=new ArrayList< > (); initListener(); }private void initListener() { doResearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(queryKey.getText())){ Toast.makeText(QueryActivity.this,"没有目标",Toast.LENGTH_SHORT).show(); return; }Cursor cursor = helper.getCost(queryKey.getText().toString()); if(cursor!=null){ while (cursor.moveToNext()){ CostBean costBean=new CostBean(); costBean.costType=cursor.getString(cursor.getColumnIndex("cost_type")); costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title")); costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date")); costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money")); list.add(costBean); } cursor.close(); }listView.setAdapter(new CostAdapter(QueryActivity.this,list)); } }); } }

QueryActivity
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> < EditText android:layout_width="match_parent" android:hint="条件" android:layout_margin="10dp" android:layout_marginTop="5dp" android:textSize="20sp" android:id="@+id/et_queryKey" android:layout_height="wrap_content"/> < Button android:layout_width="match_parent" android:text="搜索" android:textSize="20sp" android:id="@+id/bt_do_research" android:layout_margin="10dp" android:layout_height="wrap_content"/> < ListView android:layout_width="match_parent" android:id="@+id/lv_query" android:layout_margin="10dp" android:layout_height="wrap_content"/> < /LinearLayout>

activity_query
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> < TextView android:layout_width="match_parent" android:layout_margin="4dp" android:text="记账类型" android:textSize="20sp" android:layout_height="wrap_content"/> < Spinner android:layout_width="match_parent" android:layout_margin="4dp" android:entries="@array/cost_type" android:id="@+id/sp_type" android:scrollbarSize="20sp" android:layout_height="wrap_content"/> < EditText android:id="@+id/et_cost_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="Cost Title" /> < EditText android:id="@+id/et_cost_money" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="Cost Money" /> < DatePicker android:id="@+id/dp_cost_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="4dp" android:calendarViewShown="false" android:datePickerMode="spinner" /> < /LinearLayout>

new_cost
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
package com.example.daliy; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; import android.widget.Toast; import androidx.annotation.Nullable; public class SpinnerAdapterActivity extends Activity {private static final String TAG="SpinnerAdapterActivity"; public Spinner spinner; public String selectText; public SpinnerAdapterActivity(){ spinner.findViewById(R.id.sp_type); }@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView< ?> parent, View view, int position, long id) { selectText=parent.getItemAtPosition(position).toString(); //Log.d(TAG, "onItemSelected: "+selectText); }@Override public void onNothingSelected(AdapterView< ?> parent) {} }); } }

SpinnerActivity 三、效果
Android开发之记账本开发第五天

文章图片

 

  这些都是原有数据
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片

 
【Android开发之记账本开发第五天】 
Android开发之记账本开发第五天

文章图片
Android开发之记账本开发第五天

文章图片

 
使用的是模糊查询,sql语句拼接,点击delete_AllData就可以吧信息全部删除。
四、心得体会 按照昨天的思路今天的任务是可以完成的,可是下拉框竟然比我想象的难多了也可能是我想的太简单了吧,所以就在网上看别人的源代码学习,然后今天下午还看了会课,对activity进一步的学习。看来chart图表的解决得放到明天咯!发现Android并不难,就是知识点比较多,开学还要考试所以复习的也要提上日程了! 

    推荐阅读