标签:listview alert content date 今天 seh onupgrade statement als
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.view.LayoutInflater; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> costBeanList; private DatabaseHelper helper; 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); builder.setTitle("新账目"); builder.setView(viewDialog); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { CostBean costBean=new CostBean(); 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.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); startActivity(intent); intent.putExtra("cost_list", (Serializable) costBeanList); return true; } return super.onOptionsItemSelected(item); } }
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,"+ "cost_title varchar,"+ "cost_date 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); database.insert(Constants.TABLE_NAME,null,cv); } public Cursor getAllCost(){ SQLiteDatabase database=getWritableDatabase(); return database.query(Constants.TABLE_NAME,null,null,null,null,null,"cost_date asc"); } 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; 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"; }
<?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"> <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:datePickerMode="spinner" android:calendarViewShown="false" android:layout_margin="4dp" /> </LinearLayout>
标签:listview alert content date 今天 seh onupgrade statement als
原文地址:https://www.cnblogs.com/suanai/p/12291711.html