时间:2021-07-01 10:21:17 帮助过:3人阅读
DetailActivity
package com.example.tongxinlu; public class DetailActivity extends Activity { EditText edname, edphone, edmobile, edemail, edpost, edaddr, edcomp; String name, phone, mobile, email, post, addr, comp; Button btnadd; UserSQL usersql; SQLiteDatabase userdatabase; Bundle bd; String selectname; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); btnadd = (Button) findViewById(R.id.deadd); edname = (EditText) findViewById(R.id.edname); edphone = (EditText) findViewById(R.id.edphone); edmobile = (EditText) findViewById(R.id.edmoblie); edemail = (EditText) findViewById(R.id.edemail); edpost = (EditText) findViewById(R.id.edpost); edaddr = (EditText) findViewById(R.id.edaddr); edcomp = (EditText) findViewById(R.id.edcomp); usersql = new UserSQL(DetailActivity.this, "user.db", null, 1); userdatabase = usersql.getReadableDatabase(); bd = getIntent().getExtras(); if (!bd.getString("name").equals("")) { selectname = bd.getString("name"); Cursor cursor = userdatabase.rawQuery( "select * from userTable where name=?", new String[] { selectname }); cursor.moveToFirst(); edname.setText(cursor.getString(cursor.getColumnIndex("name"))); edphone.setText(cursor.getString(cursor.getColumnIndex("phone"))); edmobile.setText(cursor.getString(cursor.getColumnIndex("mobile"))); edemail.setText(cursor.getString(cursor.getColumnIndex("email"))); edpost.setText(cursor.getString(cursor.getColumnIndex("post"))); edaddr.setText(cursor.getString(cursor.getColumnIndex("addr"))); edcomp.setText(cursor.getString(cursor.getColumnIndex("comp"))); btnadd.setText("更新"); } btnadd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub name = edname.getText().toString(); phone = edphone.getText().toString(); mobile = edmobile.getText().toString(); email = edemail.getText().toString(); post = edpost.getText().toString(); addr = edaddr.getText().toString(); comp = edcomp.getText().toString(); if (!name.equals("") && !phone.equals("") && !mobile.equals("") && !email.equals("") && !post.equals("") && !addr.equals("") && !comp.equals("")) { Cursor cursor = userdatabase.rawQuery( "select * from userTable where name=?", new String[] { name }); cursor.moveToFirst(); ContentValues cv = new ContentValues(); cv.put("name", name); cv.put("phone", phone); cv.put("mobile", mobile); cv.put("email", email); cv.put("post", post); cv.put("addr", addr); cv.put("comp", comp); if (cursor.getCount() <= 0) { userdatabase.insert("userTable", null, cv); cv.clear(); Toast.makeText(DetailActivity.this, "保存" + name + "成功", Toast.LENGTH_LONG).show(); userdatabase.delete("userTable", "name=?", new String[] { selectname }); Intent it = new Intent(); it.setClass(DetailActivity.this, MainActivity.class); startActivity(it); finish(); } else if (cursor.getCount() == 1 && cursor.getString(cursor.getColumnIndex("name")) .equals(selectname)) { userdatabase.update("userTable", cv, "name=?", new String[] { selectname }); cv.clear(); Toast.makeText(DetailActivity.this, "更新" + name + "成功", Toast.LENGTH_LONG).show(); Intent it = new Intent(); it.setClass(DetailActivity.this, MainActivity.class); startActivity(it); finish(); } else { Toast.makeText(DetailActivity.this, name + "已注册", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(DetailActivity.this, "信息不完整", Toast.LENGTH_LONG).show(); } } }); } }
main
package com.example.tongxinlu;public class MainActivity extends Activity { Button btnadd, btndel; Intent it = new Intent(); ListView listview; UserSQL usersql; SQLiteDatabase userdatabases; ArrayList<HashMap<String, Object>> userlist; String[] name=new String[100]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnadd = (Button) findViewById(R.id.add); btndel = (Button) findViewById(R.id.delete); listview = (ListView) findViewById(R.id.listView1); usersql = new UserSQL(MainActivity.this, "user.db", null, 1); userdatabases = usersql.getReadableDatabase(); userlist = new ArrayList<HashMap<String, Object>>(); Cursor cursor = userdatabases.rawQuery("select * from userTable", null); cursor.moveToFirst(); if (cursor.getCount() > 0) { for (int i = 0; i < cursor.getCount(); i++) { HashMap<String, Object> hashmap = new HashMap<String, Object>(); hashmap.put("name", cursor.getString(cursor.getColumnIndex("name"))); name[i]=cursor.getString(cursor.getColumnIndex("name")); hashmap.put("phone", "["+cursor.getString(cursor.getColumnIndex("phone"))+"]"); userlist.add(hashmap); if (i < cursor.getCount()) { cursor.moveToNext(); } } SimpleAdapter sa = new SimpleAdapter(MainActivity.this, userlist, R.layout.list, new String[] { "name", "phone" }, new int[] { R.id.namelist, R.id.phonelist }); listview.setAdapter(sa); } listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,DetailActivity.class); Bundle bd=new Bundle(); bd.putString("name", name[position]); intent.putExtras(bd); startActivity(intent); } }); btnadd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub it.setClass(MainActivity.this, DetailActivity.class); Bundle bd=new Bundle(); bd.putString("name", ""); it.putExtras(bd); startActivity(it); } }); btndel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub it.setClass(MainActivity.this, DeleteActivity.class); startActivity(it); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
运行结果:(截图)
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)
经过本次的实验,对数据库进行增、删、查、改的操作有了一定的了解,通过参考资料和老师上课讲解梳理了部分知识难点,完成的挺顺利。日后需要多看,多练编写android程序。
实验评语
实验成绩
指导教师签名: 年 月 日
实验八:SQLite数据库操作
标签: