Android开发之记账本开发第七天
一、说在前面
今天是开发的第7天,今天没怎么添加新的东西,就是在原有的基础上改了一些小bug,比如增加完账目之后进行更改和删除闪退的问题,另外把自己的项目代码上传到了github,发现上传到GitHub好难诶。下载了git然后在网上学习了学习教程。下面附上学习的链接,非常详细。https://blog.csdn.net/xqhys/article/details/98113227
二、全部源程序代码
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); final ListView costList=findViewById(R.id.lv_main); initCostData(); mAdapter = new CostAdapter(this, costBeanList); costList.setAdapter(mAdapter); costList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CostBean costBean=costBeanList.get(position); //CostBean bean= (CostBean) mAdapter.getItem(costBean.id); Intent intent=new Intent(MainActivity.this,Delete_UpdateActivity.class); intent.putExtra("id",costBean.id); startActivityForResult(intent,1); } }); 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(); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //此处可以根据两个Code进行判断,本页面和结果页面跳过来的值 if (requestCode == 1 && resultCode == 2) { //String result = data.getStringExtra("result"); costBeanList.clear(); initCostData(); mAdapter.notifyDataSetChanged(); } } 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.id=cursor.getInt(cursor.getColumnIndex("id")); 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); } }
package com.example.daliy; public class Constants { public static final String TABLE_NAME="daily"; public static final int DATABASE_VERSION=1; public static final String COST_TITLE = "cost_title"; public static final String COST_DATE = "cost_date"; public static final String COST_MONEY = "cost_money"; public static final String COST_TYPE = "cost_type"; }
package com.example.daliy; import android.content.Context; import android.util.Log; 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 { String TAG="CostAdapter"; 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) { Log.d(TAG, "getItem: "+mlist.get(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); viewHolder.mTvcostType = convertView.findViewById(R.id.tv_costType); 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); viewHolder.mTvcostType.setText(bean.costType); return convertView; } private static class ViewHolder { public TextView mTvcostTitle; public TextView mTvcostDate; public TextView mTvcostMoney; public TextView mTvcostType; } }
package com.example.daliy; import java.io.Serializable; public class CostBean implements Serializable { public int id; public String costTitle; public String costDate; public String costType; public String costMoney; }
package com.example.daliy; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(@Nullable Context context) { super(context, Constants.TABLE_NAME, null, Constants.DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists daily("+ "id integer primary key autoincrement,"+ "cost_title varchar,"+ "cost_date varchar,"+ "cost_type varchar,"+ "cost_money varchar)"); } public void insertCost(CostBean costBean){ SQLiteDatabase database=getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put(Constants.COST_TITLE,costBean.costTitle); cv.put(Constants.COST_DATE,costBean.costDate); cv.put(Constants.COST_MONEY,costBean.costMoney); cv.put(Constants.COST_TYPE,costBean.costType); database.insert(Constants.TABLE_NAME,null,cv); } public void updateCost(CostBean costBean){ SQLiteDatabase database=getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put(Constants.COST_DATE,costBean.costDate); cv.put(Constants.COST_TYPE,costBean.costType); cv.put(Constants.COST_MONEY,costBean.costMoney); cv.put(Constants.COST_TITLE,costBean.costTitle); database.update(Constants.TABLE_NAME,cv,"id='"+costBean.id+"'",null); } public Cursor getAllCost(){ SQLiteDatabase database=getWritableDatabase(); return database.query(Constants.TABLE_NAME,null,null,null,null,null,"cost_date asc"); } public Cursor getCost(String key){ SQLiteDatabase database=getWritableDatabase(); return database.query(Constants.TABLE_NAME,null,"cost_title like '%"+key+"%'",null,null,null," cost_date asc"); } public Cursor getCostById(String key){ SQLiteDatabase database=getWritableDatabase(); return database.query(Constants.TABLE_NAME,null,"id= '"+key+"'",null,null,null,null); } public void deleteById(int id){ SQLiteDatabase database=getWritableDatabase(); database.delete(Constants.TABLE_NAME,"id= '"+id+"'",null); } public void deleteAllCost(){ SQLiteDatabase database=getWritableDatabase(); database.delete(Constants.TABLE_NAME,null,null); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.example.daliy; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.SpinnerAdapter; import androidx.annotation.Nullable; public class Delete_UpdateActivity extends Activity { private int id; private CostBean costBean; DatabaseHelper helper; EditText ed_title; EditText ed_money; DatePicker dp_date; Button bt_delete; Button bt_update; Spinner sp_type; String TAG="Delete_UpdateActivity"; String selectText; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete_or_update); helper=new DatabaseHelper(Delete_UpdateActivity.this); Intent intent=getIntent(); id=intent.getIntExtra("id",id); costBean=new CostBean(); Cursor cursor=helper.getCostById(String.valueOf(id)); Log.d(TAG, "onCreate: "+id+"sss"); if(cursor!=null){ cursor.moveToNext(); 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")); } initData(); initListener(); } private void initData() { ed_title = findViewById(R.id.et_cost1_title); ed_money = findViewById(R.id.et_cost1_money); dp_date = findViewById(R.id.dp_cost1_date); sp_type = findViewById(R.id.sp_1type); bt_delete = findViewById(R.id.bt_delete); bt_update = findViewById(R.id.bt_update); ed_title.setText(costBean.costTitle); ed_money.setText(costBean.costMoney); SpinnerAdapter apsAdapter = sp_type.getAdapter(); sp_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { selectText=sp_type.getSelectedItem().toString(); Log.d(TAG, "onItemSelected: +选择运行了"); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); int size = apsAdapter.getCount(); for (int i = 0; i < size; i++) { if (TextUtils.equals(costBean.costType, apsAdapter.getItem(i).toString())) { sp_type.setSelection(i,true); break; } } } private void initListener() { bt_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { helper.deleteById(id); setResult(2); finish(); } }); bt_update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { costBean=new CostBean(); costBean.id=id; costBean.costDate= dp_date.getYear()+"-"+(dp_date.getMonth()+1)+"-"+dp_date.getDayOfMonth(); costBean.costTitle=ed_title.getText().toString(); costBean.costMoney=ed_money.getText().toString(); costBean.costType=selectText; helper.updateCost(costBean); setResult(2); finish(); } }); } }
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)); } }); } }
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) { } }); } }
三、心得体会
虽然博客里面写的开发时间是7天,但我认为自己好像放在这个账本上的实际开发时间非常非常少,其中有效时间比较少。不过这个寒假过得不像之前那样碌碌无为,跟这次的病毒我认为没有太大的关系,因为在这之前我也有自己学习,不过这样自己学习变得更加理所应当了。自己做出来的这个记账本我拿来和别人的比较下发现我做的好low,不过毕竟是自己第一个app也还有一些成就感,拿着自己的app给家长看的时候他们的反应也让我感觉自己的努力没有白费,还需要加油提高自己。
四、GitHub地址