安卓开发学习01
写在前面:
今天是学习的第一天 姑且照着网上的视频教程敲了一个简单的代码 但完全不理解其中的意思 效果也算是实现了,如图:
目前只实现了增的功能 删和改在网上查阅了半天资料还是没能想通该怎么写 下面贴一下代码:
1 package com.example.daily; 2 3 import android.content.DialogInterface; 4 import android.content.Intent; 5 import android.database.Cursor; 6 import android.os.Bundle; 7 8 import com.google.android.material.floatingactionbutton.FloatingActionButton; 9 import com.google.android.material.snackbar.Snackbar; 10 11 import androidx.appcompat.app.AlertDialog; 12 import androidx.appcompat.app.AppCompatActivity; 13 import androidx.appcompat.widget.Toolbar; 14 15 import android.text.Layout; 16 import android.view.LayoutInflater; 17 import android.view.View; 18 import android.view.Menu; 19 import android.view.MenuItem; 20 import android.widget.Button; 21 import android.widget.DatePicker; 22 import android.widget.EditText; 23 import android.widget.ListView; 24 25 import java.io.Serializable; 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public class MainActivity extends AppCompatActivity { 30 31 private List<CostBean> mCostBeanList; 32 private DatabaseHelper mDatabaseHelper; 33 private CostListAdapter mAdapter; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.activity_main); 39 Toolbar toolbar = findViewById(R.id.toolbar); 40 setSupportActionBar(toolbar); 41 mDatabaseHelper = new DatabaseHelper(this,"daily",null,1); 42 mCostBeanList = new ArrayList<>(); 43 ListView costList = findViewById(R.id.lv_main); 44 initCostData(); 45 mAdapter = new CostListAdapter(this, mCostBeanList); 46 costList.setAdapter(mAdapter); 47 48 FloatingActionButton fab = findViewById(R.id.fab); 49 fab.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View view) { 52 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 53 LayoutInflater inflater = LayoutInflater.from(MainActivity.this); 54 View viewDiaglog = inflater.inflate(R.layout.new_cost_data,null); 55 final EditText title = viewDiaglog.findViewById(R.id.et_cost_title); 56 final EditText money = viewDiaglog.findViewById(R.id.et_cost_money); 57 final DatePicker date = viewDiaglog.findViewById(R.id.dp_cost_data); 58 builder.setView(viewDiaglog); 59 builder.setTitle("新的消费项"); 60 builder.setPositiveButton("添加", new DialogInterface.OnClickListener() { 61 @Override 62 public void onClick(DialogInterface dialog, int which) { 63 CostBean costBean = new CostBean(); 64 costBean.costTitle = title.getText().toString(); 65 costBean.costMoney = money.getText().toString(); 66 costBean.costDate = date.getYear() + "-" + (date.getMonth()+1)+"-" + date.getDayOfMonth(); 67 mDatabaseHelper.insertCost(costBean); 68 mCostBeanList.add(costBean); 69 mAdapter.notifyDataSetChanged(); 70 } 71 }); 72 builder.setNegativeButton("取消",null); 73 builder.create().show(); 74 } 75 }); 76 Button delbtn = findViewById(R.id.deleteCost); 77 78 } 79 80 private void initCostData() { 81 // mDatabaseHelper.deleteAllData(); 82 // for (int i = 0;i<6;i++) { 83 // CostBean costBean = new CostBean(); 84 // costBean.costTitle = i + "mock"; 85 // costBean.costDate = "11-11"; 86 // costBean.costMoney = "20"; 87 // mDatabaseHelper.insertCost(costBean); 88 // } 89 90 Cursor cursor = mDatabaseHelper.getAllCostData(); 91 if(cursor!=null){ 92 while(cursor.moveToNext()){ 93 CostBean costBean = new CostBean(); 94 costBean.costTitle = cursor.getString(cursor.getColumnIndex("cost_title")); 95 costBean.costDate = cursor.getString(cursor.getColumnIndex("cost_date")); 96 costBean.costMoney = cursor.getString(cursor.getColumnIndex("cost_money")); 97 mCostBeanList.add(costBean); 98 } 99 cursor.close(); 100 } 101 102 } 103 104 @Override 105 public boolean onCreateOptionsMenu(Menu menu) { 106 // Inflate the menu; this adds items to the action bar if it is present. 107 getMenuInflater().inflate(R.menu.menu_main, menu); 108 return true; 109 } 110 111 @Override 112 public boolean onOptionsItemSelected(MenuItem item) { 113 int id = item.getItemId(); 114 if (id == R.id.action_chart) { 115 Intent intent = new Intent(MainActivity.this,ChartActivity.class); 116 intent.putExtra("cost_list", (Serializable) mCostBeanList); 117 startActivity(intent); 118 return true; 119 } 120 121 return super.onOptionsItemSelected(item); 122 } 123 }
1 package com.example.daily; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.Button; 9 import android.widget.TextView; 10 11 import org.w3c.dom.Text; 12 13 import java.util.List; 14 15 public class CostListAdapter extends BaseAdapter{ 16 private List<CostBean> mList; 17 private Context mContext; 18 private LayoutInflater mLayoutInflater; 19 public CostListAdapter(Context context, List<CostBean> list){ 20 mContext = context; 21 mList = list; 22 mLayoutInflater = LayoutInflater.from(context); 23 } 24 25 26 27 @Override 28 public int getCount() { 29 return mList.size(); 30 } 31 32 @Override 33 public Object getItem(int position) { 34 return mList.get(position); 35 } 36 37 @Override 38 public long getItemId(int position) { 39 return position; 40 } 41 42 @Override 43 public View getView(int position, View convertView, ViewGroup parent) { 44 ViewHolder viewHolder; 45 if(convertView==null){ 46 viewHolder = new ViewHolder(); 47 convertView = mLayoutInflater.inflate(R.layout.list_item,null); 48 viewHolder.mTvCostTitle = convertView.findViewById(R.id.tv_title); 49 viewHolder.mTvCostDate = convertView.findViewById(R.id.tv_date); 50 viewHolder.mTvCostMoney = convertView.findViewById(R.id.tv_cost); 51 convertView.setTag(viewHolder); 52 }else{ 53 viewHolder = (ViewHolder) convertView.getTag(); 54 } 55 CostBean bean = mList.get(position); 56 viewHolder.mTvCostTitle.setText(bean.costTitle); 57 viewHolder.mTvCostDate.setText(bean.costDate); 58 viewHolder.mTvCostMoney.setText(bean.costMoney); 59 return convertView; 60 } 61 private static class ViewHolder{ 62 public TextView mTvCostTitle; 63 public TextView mTvCostDate; 64 public TextView mTvCostMoney; 65 } 66 67 }
1 package com.example.daily; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.database.sqlite.SQLiteOpenHelper; 8 9 import androidx.annotation.Nullable; 10 11 public class DatabaseHelper extends SQLiteOpenHelper { 12 13 public static final String COST_MONEY = "cost_money"; 14 public static final String COST_DATE = "cost_date"; 15 public static final String COST_TITLE = "cost_title"; 16 public static final String COST = "cost"; 17 public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { 18 super(context, "daily", null, 1); 19 } 20 21 @Override 22 public void onCreate(SQLiteDatabase db) { 23 db.execSQL("create table if not exists cost(" + 24 "id integer primary key AUTOINCREMENT," + 25 "cost_title varchar," + 26 "cost_date varchar," + 27 "cost_money varchar)"); 28 } 29 public void insertCost(CostBean costBean){ 30 SQLiteDatabase database = getWritableDatabase(); 31 ContentValues cv = new ContentValues(); 32 cv.put(COST_TITLE,costBean.costTitle); 33 cv.put(COST_DATE,costBean.costDate); 34 cv.put(COST_MONEY,costBean.costMoney); 35 database.insert(COST,null,cv); 36 } 37 public Cursor getAllCostData(){ 38 SQLiteDatabase database = getWritableDatabase(); 39 return database.query(COST,null,null,null,null,null,COST_DATE+" ASC"); 40 41 } 42 public void deleteAllData(){ 43 SQLiteDatabase database = getWritableDatabase(); 44 database.delete(COST,null,null); 45 } 46 public void deleteCost(int id){ 47 SQLiteDatabase database = getWritableDatabase(); 48 database.delete(COST,"id=?",new String[]{"id"}); 49 } 50 @Override 51 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 52 53 } 54 }
其他的代码都大同小异 也不再贴了