hqulyc 2020-02-13
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();
}
});
}
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);
}
}MainActivity
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.view.View;
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;
@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));
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();
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();
}
});
}
}DeleteUpdateActivity
<?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">
<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_1type"
android:scrollbarSize="20sp"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/et_cost1_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="Cost Title" />
<EditText
android:id="@+id/et_cost1_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="Cost Money" />
<DatePicker
android:id="@+id/dp_cost1_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
android:layout_width="match_parent"
android:layout_height="205dp"
android:gravity="center">
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete" />
<Button
android:id="@+id/bt_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="update" />
</LinearLayout>
</LinearLayout>delete_or_update

点击其中一条

点击相应的键会有相应的效果然后跳转回原来的界面刷新