用户寒山 發表於 2020-2-14 18:35:00

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

<h1><span style="font-family: 楷体">一、说在前面</span></h1>
<h2><span style="font-family: 楷体">今天是开发的第7天,今天没怎么添加新的东西,就是在原有的基础上改了一些小bug,比如增加完账目之后进行更改和删除闪退的问题,另外把自己的项目代码上传到了github,发现上传到GitHub好难诶。下载了git然后在网上学习了学习教程。下面附上学习的链接,非常详细。https://blog.csdn.net/xqhys/article/details/98113227</span></h2>
<h2>&nbsp;</h2>
<h1><span style="font-family: 楷体">二、全部源程序代码</span></h1>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_a8cd53c1-370c-47a7-b582-1a6d664c7c37" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_a8cd53c1-370c-47a7-b582-1a6d664c7c37" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_a8cd53c1-370c-47a7-b582-1a6d664c7c37" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.DialogInterface;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Intent;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.google.android.material.floatingactionbutton.FloatingActionButton;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.app.AlertDialog;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.app.AppCompatActivity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.appcompat.widget.Toolbar;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.text.TextUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.Log;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.LayoutInflater;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.Menu;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.MenuItem;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.AdapterView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.DatePicker;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.EditText;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.ListView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Spinner;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Toast;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.Serializable;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> MainActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AppCompatActivity {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String TAG="MainActivity"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> List&lt;CostBean&gt;<span style="color: rgba(0, 0, 0, 1)"> costBeanList;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> DatabaseHelper helper;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> String selectText="oo"<span style="color: rgba(0, 0, 0, 1)">;
    CostAdapter mAdapter;

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {

      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      costBeanList</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      helper</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> DatabaseHelper(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">final</span> ListView costList=<span style="color: rgba(0, 0, 0, 1)">findViewById(R.id.lv_main);
      initCostData();
      mAdapter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> CostAdapter(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, costBeanList);
      costList.setAdapter(mAdapter);
      costList.setOnItemClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AdapterView.OnItemClickListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onItemClick(AdapterView&lt;?&gt; parent, View view, <span style="color: rgba(0, 0, 255, 1)">int</span> position, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id) {
                CostBean costBean</span>=<span style="color: rgba(0, 0, 0, 1)">costBeanList.get(position);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">CostBean bean= (CostBean) mAdapter.getItem(costBean.id);</span>
                Intent intent=<span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,Delete_UpdateActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
                intent.putExtra(</span>"id"<span style="color: rgba(0, 0, 0, 1)">,costBean.id);
                startActivityForResult(intent,</span>1<span style="color: rgba(0, 0, 0, 1)">);

            }
      });
      FloatingActionButton fab </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.fab);
      fab.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View view) {
                AlertDialog.Builder builder</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> AlertDialog.Builder(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
                LayoutInflater inflater</span>=LayoutInflater.from(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
                View viewDialog </span>=inflater.inflate(R.layout.new_cost,<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">final</span> EditText cost_title=<span style="color: rgba(0, 0, 0, 1)">viewDialog.findViewById(R.id.et_cost_title);
                </span><span style="color: rgba(0, 0, 255, 1)">final</span> EditText cost_money=<span style="color: rgba(0, 0, 0, 1)">viewDialog.findViewById(R.id.et_cost_money);
                </span><span style="color: rgba(0, 0, 255, 1)">final</span> DatePicker cost_date=<span style="color: rgba(0, 0, 0, 1)">viewDialog.findViewById(R.id.dp_cost_date);
                </span><span style="color: rgba(0, 0, 255, 1)">final</span> Spinner cost_type=<span style="color: rgba(0, 0, 0, 1)">viewDialog.findViewById(R.id.sp_type);
                cost_type.setOnItemSelectedListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AdapterView.OnItemSelectedListener() {
                  @Override
                  </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onItemSelected(AdapterView&lt;?&gt; parent, View view, <span style="color: rgba(0, 0, 255, 1)">int</span> position, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id) {
                        selectText</span>=<span style="color: rgba(0, 0, 0, 1)">cost_type.getSelectedItem().toString();
                        Log.d(TAG, </span>"onItemSelected: +选择运行了"<span style="color: rgba(0, 0, 0, 1)">);
                  }

                  @Override
                  </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onNothingSelected(AdapterView&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)"> parent) {

                  }
                });
                builder.setTitle(</span>"新账目"<span style="color: rgba(0, 0, 0, 1)">);
                builder.setView(viewDialog);
                builder.setPositiveButton(</span>"OK", <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DialogInterface.OnClickListener() {
                  @Override
                  </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onClick(DialogInterface dialog, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> which) {
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(TextUtils.isEmpty(cost_title.getText())){
                            Toast.makeText(MainActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>,"备注为空"<span style="color: rgba(0, 0, 0, 1)">,Toast.LENGTH_SHORT).show();
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                        }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(TextUtils.isEmpty(cost_money.getText())){
                            Toast.makeText(MainActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>,"金额为空"<span style="color: rgba(0, 0, 0, 1)">,Toast.LENGTH_SHORT).show();
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                        }
                        CostBean costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
                        costBean.costType</span>=<span style="color: rgba(0, 0, 0, 1)">selectText;
                        Log.d(TAG, </span>"onClick: "+<span style="color: rgba(0, 0, 0, 1)">selectText);
                        costBean.costTitle</span>=<span style="color: rgba(0, 0, 0, 1)">cost_title.getText().toString();
                        costBean.costMoney</span>=<span style="color: rgba(0, 0, 0, 1)">cost_money.getText().toString();
                        costBean.costDate</span>=cost_date.getYear()+"-"+(cost_date.getMonth()+1)+"-"+<span style="color: rgba(0, 0, 0, 1)">cost_date.getDayOfMonth();
                        helper.insertCost(costBean);
                        costBeanList.add(costBean);
                        mAdapter.notifyDataSetChanged();
                  }
                });
                builder.setNegativeButton(</span>"Cancel",<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
                builder.create().show();
            }
      });
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onActivityResult(<span style="color: rgba(0, 0, 255, 1)">int</span> requestCode, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> resultCode,Intent data) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onActivityResult(requestCode, resultCode, data);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">此处可以根据两个Code进行判断,本页面和结果页面跳过来的值</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (requestCode == 1 &amp;&amp; resultCode == 2<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">String result = data.getStringExtra("result");</span>
<span style="color: rgba(0, 0, 0, 1)">            costBeanList.clear();
            initCostData();
            mAdapter.notifyDataSetChanged();
      }
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initCostData() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      helper.deleteAllCost();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      for(int i=0;i&lt;6;i++) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">            CostBean cb=new CostBean();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">            cb.costDate="12-12";
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">            cb.costMoney="50";
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">            cb.costTitle=i+"heih";
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">            helper.insertCost(cb);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      }</span>
      Cursor cursor =<span style="color: rgba(0, 0, 0, 1)"> helper.getAllCost();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(cursor!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
            </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (cursor.moveToNext()){
                CostBean costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
                costBean.id</span>=cursor.getInt(cursor.getColumnIndex("id"<span style="color: rgba(0, 0, 0, 1)">));
                costBean.costType</span>=cursor.getString(cursor.getColumnIndex("cost_type"<span style="color: rgba(0, 0, 0, 1)">));
                costBean.costTitle</span>=cursor.getString(cursor.getColumnIndex("cost_title"<span style="color: rgba(0, 0, 0, 1)">));
                costBean.costDate</span>=cursor.getString(cursor.getColumnIndex("cost_date"<span style="color: rgba(0, 0, 0, 1)">));
                costBean.costMoney</span>=cursor.getString(cursor.getColumnIndex("cost_money"<span style="color: rgba(0, 0, 0, 1)">));
                costBeanList.add(costBean);
            }
            cursor.close();
      }
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onCreateOptionsMenu(Menu menu) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Inflate the menu; this adds items to the action bar if it is present.</span>
<span style="color: rgba(0, 0, 0, 1)">      getMenuInflater().inflate(R.menu.menu_main, menu);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> onOptionsItemSelected(MenuItem item) {
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> id =<span style="color: rgba(0, 0, 0, 1)"> item.getItemId();

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">noinspection SimplifiableIfStatement</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (id ==<span style="color: rgba(0, 0, 0, 1)"> R.id.action_chart) {

            Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,ChartActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            intent.putExtra(</span>"cost_list"<span style="color: rgba(0, 0, 0, 1)">, (Serializable) costBeanList);
            startActivity(intent);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(id==<span style="color: rgba(0, 0, 0, 1)">R.id.delete_allData){
            helper.deleteAllCost();
            costBeanList.clear();
            mAdapter.notifyDataSetChanged();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(id==<span style="color: rgba(0, 0, 0, 1)">R.id.query){
            Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,QueryActivity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            startActivity(intent);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onOptionsItemSelected(item);
    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">MainActivity</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_8473c56b-18ce-48a1-b6cb-04359e20da7e" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_8473c56b-18ce-48a1-b6cb-04359e20da7e" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_8473c56b-18ce-48a1-b6cb-04359e20da7e" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Constants {
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String TABLE_NAME="daily"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> DATABASE_VERSION=1<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_TITLE = "cost_title"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_DATE = "cost_date"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_MONEY = "cost_money"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String COST_TYPE = "cost_type"<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<span class="cnblogs_code_collapse">Constants</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_55405ee1-491c-4235-ae81-fee0d9a5a716" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_55405ee1-491c-4235-ae81-fee0d9a5a716" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_55405ee1-491c-4235-ae81-fee0d9a5a716" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Context;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.Log;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.LayoutInflater;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.ViewGroup;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.BaseAdapter;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.TextView;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> CostAdapter <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> BaseAdapter {

    String TAG</span>="CostAdapter"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> List&lt;CostBean&gt;<span style="color: rgba(0, 0, 0, 1)"> mlist;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> LayoutInflater minflater;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Context mcontext;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> CostAdapter(Context context, List&lt;CostBean&gt;<span style="color: rgba(0, 0, 0, 1)"> list) {
      mlist </span>=<span style="color: rgba(0, 0, 0, 1)"> list;
      mcontext </span>=<span style="color: rgba(0, 0, 0, 1)"> context;
      minflater </span>=<span style="color: rgba(0, 0, 0, 1)"> LayoutInflater.from(context);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getCount() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> mlist.size();
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Object getItem(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position) {
      Log.d(TAG, </span>"getItem: "+<span style="color: rgba(0, 0, 0, 1)">mlist.get(position));
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> mlist.get(position);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span> getItemId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> position;
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> View getView(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (convertView == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            viewHolder </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ViewHolder();
            convertView </span>= minflater.inflate(R.layout.list_item, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            viewHolder.mTvcostTitle </span>=<span style="color: rgba(0, 0, 0, 1)"> convertView.findViewById(R.id.tv_costTitle);
            viewHolder.mTvcostMoney </span>=<span style="color: rgba(0, 0, 0, 1)"> convertView.findViewById(R.id.tv_costMoney);
            viewHolder.mTvcostDate </span>=<span style="color: rgba(0, 0, 0, 1)"> convertView.findViewById(R.id.tv_costDate);
            viewHolder.mTvcostType </span>=<span style="color: rgba(0, 0, 0, 1)"> convertView.findViewById(R.id.tv_costType);
            convertView.setTag(viewHolder);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            viewHolder </span>=<span style="color: rgba(0, 0, 0, 1)"> (ViewHolder) convertView.getTag();
      }
      CostBean bean </span>=<span style="color: rgba(0, 0, 0, 1)"> mlist.get(position);
      viewHolder.mTvcostTitle.setText(bean.costTitle);
      viewHolder.mTvcostMoney.setText(bean.costMoney);
      viewHolder.mTvcostDate.setText(bean.costDate);
      viewHolder.mTvcostType.setText(bean.costType);

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> convertView;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ViewHolder {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView mTvcostTitle;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView mTvcostDate;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView mTvcostMoney;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView mTvcostType;
    }


}</span></pre>
</div>
<span class="cnblogs_code_collapse">CostAdapter</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_7b4c8191-0121-4e71-be29-435b15519b03" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_7b4c8191-0121-4e71-be29-435b15519b03" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_7b4c8191-0121-4e71-be29-435b15519b03" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.Serializable;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> CostBean <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Serializable {
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costTitle;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costDate;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costType;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String costMoney;

}</span></pre>
</div>
<span class="cnblogs_code_collapse">CostBean</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_5327e578-3440-41aa-9039-887190d594a0" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_5327e578-3440-41aa-9039-887190d594a0" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_5327e578-3440-41aa-9039-887190d594a0" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.ContentValues;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Context;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.sqlite.SQLiteDatabase;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.sqlite.SQLiteOpenHelper;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> DatabaseHelper <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteOpenHelper {

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> DatabaseHelper(@Nullable Context context) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span>(context, Constants.TABLE_NAME, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">, Constants.DATABASE_VERSION);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(SQLiteDatabase db) {
      db.execSQL(</span>"create table if not exists daily("+
                "id integer primary key autoincrement,"+
                "cost_title varchar,"+
                "cost_date varchar,"+
                "cost_type varchar,"+
                "cost_money varchar)"<span style="color: rgba(0, 0, 0, 1)">);
    }


    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> insertCost(CostBean costBean){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      ContentValues cv</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> 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,</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,cv);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateCost(CostBean costBean){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();

      ContentValues cv</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> 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,</span>"id='"+costBean.id+"'",<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Cursor getAllCost(){

      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> database.query(Constants.TABLE_NAME,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,"cost_date asc"<span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Cursor getCost(String key){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> database.query(Constants.TABLE_NAME,<span style="color: rgba(0, 0, 255, 1)">null</span>,"cost_title like '%"+key+"%'",<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>," cost_date asc"<span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Cursor getCostById(String key){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> database.query(Constants.TABLE_NAME,<span style="color: rgba(0, 0, 255, 1)">null</span>,"id= '"+key+"'",<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> deleteById(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      database.delete(Constants.TABLE_NAME,</span>"id= '"+id+"'",<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteAllCost(){
      SQLiteDatabase database</span>=<span style="color: rgba(0, 0, 0, 1)">getWritableDatabase();
      database.delete(Constants.TABLE_NAME,</span><span style="color: rgba(0, 0, 255, 1)">null</span>,<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);

    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onUpgrade(SQLiteDatabase db, <span style="color: rgba(0, 0, 255, 1)">int</span> oldVersion, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> newVersion) {

    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">DatabaseHelper</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_eaae488b-0e38-40b7-bccd-d3700b00a11a" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_eaae488b-0e38-40b7-bccd-d3700b00a11a" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_eaae488b-0e38-40b7-bccd-d3700b00a11a" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.app.Activity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.content.Intent;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.text.TextUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.Log;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.AdapterView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Button;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.DatePicker;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.EditText;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Spinner;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.SpinnerAdapter;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Delete_UpdateActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Activity {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> id;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> CostBean costBean;
    DatabaseHelper helper;
    EditText ed_title;
    EditText ed_money;
    DatePicker dp_date;
    Button bt_delete;
    Button bt_update;
    Spinner sp_type;
    String TAG</span>="Delete_UpdateActivity"<span style="color: rgba(0, 0, 0, 1)">;
    String selectText;

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(@Nullable Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      setContentView(R.layout.activity_delete_or_update);
      helper</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> DatabaseHelper(Delete_UpdateActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
      Intent intent</span>=<span style="color: rgba(0, 0, 0, 1)">getIntent();
      id</span>=intent.getIntExtra("id"<span style="color: rgba(0, 0, 0, 1)">,id);
      costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
      Cursor cursor</span>=<span style="color: rgba(0, 0, 0, 1)">helper.getCostById(String.valueOf(id));
      Log.d(TAG, </span>"onCreate: "+id+"sss"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(cursor!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
            cursor.moveToNext();
            costBean.costType</span>=cursor.getString(cursor.getColumnIndex("cost_type"<span style="color: rgba(0, 0, 0, 1)">));
            costBean.costTitle</span>=cursor.getString(cursor.getColumnIndex("cost_title"<span style="color: rgba(0, 0, 0, 1)">));
            costBean.costDate</span>=cursor.getString(cursor.getColumnIndex("cost_date"<span style="color: rgba(0, 0, 0, 1)">));
            costBean.costMoney</span>=cursor.getString(cursor.getColumnIndex("cost_money"<span style="color: rgba(0, 0, 0, 1)">));
      }

      initData();
      initListener();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initData() {
      ed_title </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.et_cost1_title);
      ed_money </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.et_cost1_money);
      dp_date </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.dp_cost1_date);
      sp_type </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.sp_1type);
      bt_delete </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.bt_delete);
      bt_update </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.bt_update);
      ed_title.setText(costBean.costTitle);
      ed_money.setText(costBean.costMoney);
      SpinnerAdapter apsAdapter </span>=<span style="color: rgba(0, 0, 0, 1)"> sp_type.getAdapter();
      sp_type.setOnItemSelectedListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AdapterView.OnItemSelectedListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onItemSelected(AdapterView&lt;?&gt; parent, View view, <span style="color: rgba(0, 0, 255, 1)">int</span> position, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id) {
                selectText</span>=<span style="color: rgba(0, 0, 0, 1)">sp_type.getSelectedItem().toString();
                Log.d(TAG, </span>"onItemSelected: +选择运行了"<span style="color: rgba(0, 0, 0, 1)">);
            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onNothingSelected(AdapterView&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)"> parent) {

            }
      });
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> size =<span style="color: rgba(0, 0, 0, 1)"> apsAdapter.getCount();
      </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; size; i++<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (TextUtils.equals(costBean.costType, apsAdapter.getItem(i).toString())) {
                sp_type.setSelection(i,</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }
      }
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initListener() {

      bt_delete.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
                helper.deleteById(id);
                setResult(</span>2<span style="color: rgba(0, 0, 0, 1)">);
                finish();
            }
      });
      bt_update.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
                costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
                costBean.id</span>=<span style="color: rgba(0, 0, 0, 1)">id;
                costBean.costDate</span>= dp_date.getYear()+"-"+(dp_date.getMonth()+1)+"-"+<span style="color: rgba(0, 0, 0, 1)">dp_date.getDayOfMonth();
                costBean.costTitle</span>=<span style="color: rgba(0, 0, 0, 1)">ed_title.getText().toString();
                costBean.costMoney</span>=<span style="color: rgba(0, 0, 0, 1)">ed_money.getText().toString();
                costBean.costType</span>=<span style="color: rgba(0, 0, 0, 1)">selectText;
                helper.updateCost(costBean);
                setResult(</span>2<span style="color: rgba(0, 0, 0, 1)">);
                finish();
            }
      });
    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">DeleteUpdateActivity</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_1c162c08-51cb-4b4a-b9e3-90b40ec9beda" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_1c162c08-51cb-4b4a-b9e3-90b40ec9beda" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_1c162c08-51cb-4b4a-b9e3-90b40ec9beda" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.app.Activity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.database.Cursor;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.text.TextUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Button;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.EditText;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.ListView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Toast;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> QueryActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Activity {
    EditText queryKey;
    Button doResearch;
    DatabaseHelper helper;
    ListView listView;
    List</span>&lt;CostBean&gt;<span style="color: rgba(0, 0, 0, 1)"> list;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">private String queryKey;</span>
<span style="color: rgba(0, 0, 0, 1)">
    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(@Nullable Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      setContentView(R.layout.activity_query_layout);
      listView</span>=<span style="color: rgba(0, 0, 0, 1)">findViewById(R.id.lv_query);
      queryKey </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.et_queryKey);
      doResearch </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.bt_do_research);
      helper</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> DatabaseHelper(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
      list</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      initListener();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initListener() {
      doResearch.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(TextUtils.isEmpty(queryKey.getText())){
                  Toast.makeText(QueryActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>,"没有目标"<span style="color: rgba(0, 0, 0, 1)">,Toast.LENGTH_SHORT).show();
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                }

                Cursor cursor </span>=<span style="color: rgba(0, 0, 0, 1)"> helper.getCost(queryKey.getText().toString());
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(cursor!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                  </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (cursor.moveToNext()){
                        CostBean costBean</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> CostBean();
                        costBean.costType</span>=cursor.getString(cursor.getColumnIndex("cost_type"<span style="color: rgba(0, 0, 0, 1)">));
                        costBean.costTitle</span>=cursor.getString(cursor.getColumnIndex("cost_title"<span style="color: rgba(0, 0, 0, 1)">));
                        costBean.costDate</span>=cursor.getString(cursor.getColumnIndex("cost_date"<span style="color: rgba(0, 0, 0, 1)">));
                        costBean.costMoney</span>=cursor.getString(cursor.getColumnIndex("cost_money"<span style="color: rgba(0, 0, 0, 1)">));
                        list.add(costBean);
                  }
                  cursor.close();
                }

                listView.setAdapter(</span><span style="color: rgba(0, 0, 255, 1)">new</span> CostAdapter(QueryActivity.<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">,list));
            }
      });
    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">QueryActivity</span></div>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" id="code_img_closed_84432614-bc4c-44e9-b49a-2dbf9868cc10" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" id="code_img_opened_84432614-bc4c-44e9-b49a-2dbf9868cc10" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_84432614-bc4c-44e9-b49a-2dbf9868cc10" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.example.daliy;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.app.Activity;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.os.Bundle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.util.Log;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.view.View;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.AdapterView;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Spinner;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> android.widget.Toast;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> androidx.annotation.Nullable;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SpinnerAdapterActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Activity {

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String TAG="SpinnerAdapterActivity"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Spinner spinner;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String selectText;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> SpinnerAdapterActivity(){
      spinner.findViewById(R.id.sp_type);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(@Nullable Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      spinner.setOnItemSelectedListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AdapterView.OnItemSelectedListener() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onItemSelected(AdapterView&lt;?&gt; parent, View view, <span style="color: rgba(0, 0, 255, 1)">int</span> position, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id) {
                selectText</span>=<span style="color: rgba(0, 0, 0, 1)">parent.getItemAtPosition(position).toString();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Log.d(TAG, "onItemSelected: "+selectText);</span>
<span style="color: rgba(0, 0, 0, 1)">            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onNothingSelected(AdapterView&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)"> parent) {

            }
      });
    }
}</span></pre>
</div>
<span class="cnblogs_code_collapse">SpinnerActivity</span></div>
<h1><span style="font-family: 楷体">三、心得体会</span></h1>
<h2><span style="font-family: 楷体">虽然博客里面写的开发时间是7天,但我认为自己好像放在这个账本上的实际开发时间非常非常少,其中有效时间比较少。不过这个寒假过得不像之前那样碌碌无为,跟这次的病毒我认为没有太大的关系,因为在这之前我也有自己学习,不过这样自己学习变得更加理所应当了。自己做出来的这个记账本我拿来和别人的比较下发现我做的好low,不过毕竟是自己第一个app也还有一些成就感,拿着自己的app给家长看的时候他们的反应也让我感觉自己的努力没有白费,还需要加油提高自己。</span></h2>
<p>&nbsp;</p>
<h1><span style="font-family: 楷体">四、GitHub地址</span></h1>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/suanai/p/12308494.html
頁: [1]
查看完整版本: Android开发之记账本开发第七天