创建一个实体类声明变量以及其get和set方法
一个openhelper创建数据库更新版本等操作
两个活动,一个主活动输入数据,进行添加,更新,删除的操作
另一个活动显示查询的数据在自定义的listview里
public class MainActivity extends AppCompatActivity { private EditText edit_name; private EditText edit_phone; private MyOpenHelper myOpenHelper; private Listlists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取实例 myOpenHelper = new MyOpenHelper(getApplicationContext()); myOpenHelper.getReadableDatabase(); //创建实体类集合 lists = new ArrayList (); edit_name=findViewById(R.id.edit_name); edit_phone=findViewById(R.id.edit_phone); } //增加 public void click1(View v) { String db_name=edit_name.getText().toString().trim(); String db_phone=edit_phone.getText().toString().trim(); //获取数据库对象 SQLiteDatabase db = myOpenHelper.getWritableDatabase(); ContentValues values1 = new ContentValues(); values1.put("car",db_name); values1.put("way",db_phone); values1.put("address","湖南省长沙市天心区"); values1.put("time","2018-08-16 15:47:00"); long insert; if(TextUtils.isEmpty(db_name) || TextUtils.isEmpty(db_phone)) { insert=0; }else{ long insert1 = db.insert("info", null, values1); insert=insert1; } Log.d("0013","insert is "+insert); //[3]数据库用完需要关闭 db.close(); if (insert>0) { Toast.makeText(getApplicationContext(), "插入成功", Toast.LENGTH_LONG).show(); Log.d("001", "添加成功~! & insert : "+String.valueOf(values1)); } else { Toast.makeText(getApplicationContext(),"插入失败,插入数据不能为空",Toast.LENGTH_LONG).show(); Log.d("001", "插入失败,插入数据不能为空"); } } //删除 public void click2(View v) { SQLiteDatabase db = myOpenHelper.getWritableDatabase(); int delete = db.delete("info", "car=?", new String[]{"宁ASD123"}); db.close(); } //更新 public void click3(View v) { SQLiteDatabase db = myOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("way", "电话"); int updata = db.update("info", values, "car=?", new String[]{"宁ASD123"}); db.close(); } //查询 public void click4(View v) { Intent intent=new Intent(MainActivity.this,NewActivity.class); startActivity(intent); } }
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class NewActivity extends AppCompatActivity{ private Listlists; private ListView lv1; private MyOpenHelper myOpenHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); lists = new ArrayList (); lv1 = findViewById(R.id.list1); myOpenHelper = new MyOpenHelper(getApplicationContext()); SQLiteDatabase db = myOpenHelper.getWritableDatabase(); lists.clear(); Cursor cursor = db.query("info", null, null, null,null, null, null); if (cursor!=null&&cursor.getCount()>0){ int querynum =0;//放到前面全局变量就每次都叠加,不清楚上一次查询结果,是错误的 Log.d("0020","开始查询啦,以下为一次查询结果呦~!"); while(cursor.moveToNext()){ Person person=new Person(); person.setCar(cursor.getString(1)); person.setWay(cursor.getString(2)); person.setAddress(cursor.getString(3)); person.setTime(cursor.getString(4)); querynum++; Log.d("002","query result : "+person.getCar()+" , "+person.getWay()+" , "+person.getAddress()+" , "+person.getTime()); Log.d("003","query number is "+querynum); //把javabean对象加入到集合中 lists.add(person); } lv1.setAdapter(new MyAdepter()); Log.d("0021","此次查询结束啦~!"); } db.close(); } public class MyAdepter extends BaseAdapter { @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v; if (convertView == null) { v = View.inflate(getApplicationContext(), R.layout.item_record, null); } else { v=convertView; } TextView tv_car = v.findViewById(R.id.tv_car); TextView tv_way = v.findViewById(R.id.tv_way); TextView tv_addr = v.findViewById(R.id.tv_addr); TextView tv_time = v.findViewById(R.id.tv_time); Person person=lists.get(position); tv_car.setText(person.getCar()); tv_way.setText(person.getWay()); tv_addr.setText(person.getAddress()); tv_time.setText(person.getTime()); return v; } } }
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "record.db", null,2); } public void onCreate(SQLiteDatabase db) { db.execSQL("create table info(_id integer primary key autoincrement,car varchar(30),way varchar(30),address varchar(80),time varchar(50))"); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }