Android高级-Android操作SQL数据管理,增删改查

曾无好事来相访,赖尔高文一起予。这篇文章主要讲述Android高级-Android操作SQL数据管理,增删改查相关的知识,希望能为你提供帮助。
 
已经学了好几天SQL了,昨天刚接触到android操作SQL数据库,晚上老师留了一个作业,效果图如下

Android高级-Android操作SQL数据管理,增删改查

文章图片

Android高级-Android操作SQL数据管理,增删改查

文章图片
Android高级-Android操作SQL数据管理,增删改查

文章图片
Android高级-Android操作SQL数据管理,增删改查

文章图片

分别是,主界面,和修改,添加,以及删除界面
首先我们先来实现布局 MainActivity.xml
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 4android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" 5 6tools:context="wuxianedu.com.sqllistview.MainActivity"> 7 8< ListView android:id="@+id/lv_liebiao" 9android:layout_width="match_parent" 10android:layout_height="match_parent"> 11< /ListView> 12 < /RelativeLayout>

布局很简洁,只有一个ListView,
【Android高级-Android操作SQL数据管理,增删改查】下面是List列表的item布局
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:orientation="horizontal" android:layout_width="match_parent" 4android:layout_height="match_parent"> 5< LinearLayoutandroid:layout_width="0dp" 6android:layout_height="wrap_content" 7android:layout_weight="5"> 8< TextView android:id="@+id/tv_name" 9android:layout_width="0dp" android:layout_gravity="center_vertical" 10android:layout_height="wrap_content" 11android:text="aaa" android:layout_weight="1" 12android:gravity="center"/> 13 14< TextView android:id="@+id/tv_age" 15android:layout_width="wrap_content" 16android:layout_height="wrap_content" android:layout_gravity="center_vertical" 17android:text="aaa" android:layout_weight="1" 18android:gravity="center"/> 19< TextView android:id="@+id/tv_sex" 20android:layout_width="wrap_content" 21android:layout_height="wrap_content" android:layout_gravity="center_vertical" 22android:text="aaa" android:layout_weight="1" 23android:gravity="center"/> 24< /LinearLayout> 25< Button android:id="@+id/but_name" 26android:layout_width="0dp" 27android:layout_height="wrap_content" 28android:text="修改" android:layout_weight="1" 29android:gravity="center"/> 30 31< Button android:id="@+id/but_delete" 32android:layout_width="0dp" 33android:layout_height="wrap_content" 34android:text="删除" android:layout_weight="1" 35android:gravity="center"/> 36 < /LinearLayout>

之后是修改和添加界面的XML代码
UpdateActivity.xml


1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:orientation="vertical" 6android:layout_height="match_parent" 7android:paddingBottom="@dimen/activity_vertical_margin" 8android:paddingLeft="@dimen/activity_horizontal_margin" 9android:paddingRight="@dimen/activity_horizontal_margin" 10android:paddingTop="@dimen/activity_vertical_margin" 11tools:context="wuxianedu.com.sqllistview.UpdateActivity"> 12 13< EditText android:id="@+id/ed_name" 14android:layout_width="match_parent" 15android:layout_height="wrap_content" 16android:hint="请输入姓名"/> 17 18< EditText android:id="@+id/ed_age" 19android:layout_width="match_parent" 20android:layout_height="wrap_content" 21android:hint="请输入年龄" 22android:layout_below="@+id/ed_name"/> 23 24< EditText android:id="@+id/ed_sex" 25android:layout_width="match_parent" 26android:layout_height="wrap_content" 27android:hint="请输入性别" 28android:layout_below="@+id/ed_age"/> 29 30< Button android:id="@+id/but_queding" 31android:layout_width="match_parent" 32android:layout_height="wrap_content" 33android:hint="修改用户" 34android:layout_below="@+id/ed_sex"/> 35 36< Button android:id="@+id/but_tianjia" 37android:layout_width="match_parent" 38android:layout_height="wrap_content" 39android:hint="添加用户" 40android:layout_below="@+id/ed_sex"/> 41 42 43 < /RelativeLayout >

这里要说一下,添加数据和修改数据用的是一个布局,在JAva文件中通过判断来确定是修改还是添加,
下面贴出JAVA代码
 
1 package wuxianedu.com.sqllistview; 2 3 import android.content.ContentValues; 4 import android.content.Intent; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.MenuItem; 10 import android.widget.ListView; 11 12 import java.util.List; 13 14 import wuxianedu.com.sqllistview.DB.DBauxiliary; 15 16 public class MainActivity extends AppCompatActivity { 17 18private MainAdapter mainAdapter; //构建数据源 19private List list; //接收数据的LIST 20private DBauxiliary db; //数据库操作类的实例 21 22@Override 23protected void onCreate(Bundle savedInstanceState) { 24super.onCreate(savedInstanceState); 25setContentView(R.layout.activity_main); 26ListView listView = (ListView) findViewById(R.id.lv_liebiao); 27db = DBauxiliary.getInstance(this); 28list= db.select(); 29//构建数据源 30mainAdapter = new MainAdapter(this,list); 31//添加数据 32listView.setAdapter(mainAdapter); 33} 34//Activity创建或者从被覆盖、后台重新回到前台时被调用 35@Override 36protected void onResume() { 37super.onResume(); 38//重新获取list数据 39list = db.select(); 40//给list赋值 更新 41mainAdapter.setList(list); 42} 43 44@Override //菜单的点击事件 45public boolean onCreateOptionsMenu(Menu menu) { 46getMenuInflater().inflate(R.menu.menu,menu); 47menu.findItem(R.id.add).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 48@Override 49public boolean onMenuItemClick(MenuItem item) { 50//点击添加跳转 51Intent intent=newIntent(MainActivity.this,UpdateActivity.class); 52startActivityForResult(intent,5); 53return false; 54} 55}); 56return super.onCreateOptionsMenu(menu); 57 58} 59 }

 
MainAdapter.java
此文件用来填充Adapter数据


1 package wuxianedu.com.sqllistview; 2 3 import android.content.Context; 4 import android.content.DialogInterface; 5 import android.content.Intent; 6 import android.support.v7.app.AlertDialog; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.BaseAdapter; 11 import android.widget.Button; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 import java.util.List; 16 17 import wuxianedu.com.sqllistview.DB.DBauxiliary; 18 19 /** 20* Created by Administrator on 2016/9/20. 21*/ 22 public class MainAdapter extends BaseAdapter { 23private Context context; 24private List< JavaBean> list; //接收数据的LIST 25private DBauxiliary db; //数据库操作类的实例 26//适配器 27public MainAdapter(Context context,List< JavaBean> list){ 28this.context = context; 29this.list = list; 30} 31//更新集合用 32public void setList(List< JavaBean> list) { 33this.list = list; 34notifyDataSetChanged(); 35} 36 37@Override 38public int getCount() { 39return list.size(); 40} 41 42@Override 43public Object getItem(int position) { 44return list.get(position); 45} 46 47@Override 48public long getItemId(int position) { 49return position; 50} 51 52@Override 53public View getView(final int position, View convertView, ViewGroup parent) { 54final HUi hui; 55if(convertView == null){ 56hui = new HUi(); 57final int wo ; 58wo = position; 59db = DBauxiliary.getInstance(context); 60convertView = LayoutInflater.from(context).inflate(R.layout.item_lie, null); 61hui.name = (TextView) convertView.findViewById(R.id.tv_name); 62hui.age = (TextView) convertView.findViewById(R.id.tv_age); 63hui.sex = (TextView) convertView.findViewById(R.id.tv_sex); 64hui.update = (Button) convertView.findViewById(R.id.but_name); 65hui.delete = (Button) convertView.findViewById(R.id.but_delete); 66 67// 68//修改按钮 69hui.update.setOnClickListener(new View.OnClickListener() { 70@Override 71public void onClick(View v) { 72Intent intent = new Intent(context,UpdateActivity.class); 73int on = (int) hui.update.getTag(); 74JavaBean javabean = list.get(on); 75intent.putExtra("name",javabean); 76context.startActivity(intent); 77 78} 79}); 80convertView.setTag(hui); 81}else{ 82hui = (HUi) convertView.getTag(); 83} 84 85final JavaBean java = list.get(position); 86hui.update.setTag(position); 87hui.name.setText(java.getName()); 88hui.age.setText(String.valueOf(java.getAge())); 89hui.sex.setText(java.getSex()); 90final String naaa = java.getName(); 91//构建删除对话框 92hui.delete.setOnClickListener(new View.OnClickListener() { 93@Override 94public void onClick(View v) { 95AlertDialog.Builder builder = newAlertDialog.Builder(context); 96builder.setMessage("确定删除吗?"); 97builder.setPositiveButton("取消",null); 98builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { 99@Override 100public void onClick(DialogInterface dialog, int which) { 101db.delete(naaa); 102list = db.select(); 103setList(list); 104notifyDataSetChanged(); 105Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show(); 106} 107}).show(); 108 109} 110}); 111return convertView; 112} 113 114class HUi { 115TextView name,age,sex; 116Button update,delete; 117} 118 }

 
UpdateActivity.java
用于添加和修改数据


1 package wuxianedu.com.sqllistview; 2 3 import android.content.ContentValues; 4 import android.content.Intent; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 import wuxianedu.com.sqllistview.DB.DBauxiliary; 13 14 public class UpdateActivity extends AppCompatActivity implements View.OnClickListener { 15 16private DBauxiliary db; 17private EditText ed_name,ed_sex,ed_age; 18private JavaBean com; 19private Button xiugai,shanchu; 20 21@Override 22protected void onCreate(Bundle savedInstanceState) { 23super.onCreate(savedInstanceState); 24setContentView(R.layout.activity_update); 25ed_name= (EditText) findViewById(R.id.ed_name); 26ed_age = (EditText) findViewById(R.id.ed_age); 27ed_sex = (EditText) findViewById(R.id.ed_sex); 28xiugai = (Button) findViewById(R.id.but_tianjia); 29shanchu = (Button) findViewById(R.id.but_queding); 30Intent on = getIntent(); 31com = (JavaBean) on.getSerializableExtra("name"); 32if(com == null){ 33shanchu.setVisibility(View.GONE); 34}else{ 35xiugai.setVisibility(View.GONE); 36ed_name.setText(com.getName()+""); 37ed_age.setText(String.valueOf(com.getAge())+""); 38ed_sex.setText(com.getSex()+""); 39} 40db = DBauxiliary.getInstance(UpdateActivity.this); 41 /*ed_name.setText(com.getName()+""); 42ed_age.setText(String.valueOf(com.getAge())+""); 43ed_sex.setText(com.getSex()+""); */ 44xiugai.setOnClickListener(this); 45shanchu.setOnClickListener(this); 46 47} 48private void SqlInsert(){ 49ContentValues contentValues = new ContentValues(); 50contentValues.put("name",ed_name.getText().toString()); 51contentValues.put("age",ed_age.getText().toString()); 52contentValues.put("sex",ed_sex.getText().toString()); 53db.insert(contentValues); 54Toast.makeText(UpdateActivity.this,"添加成功",Toast.LENGTH_SHORT).show(); 55} 56//删除数据 57private void SqlUpdate(){ 58ContentValues contentValues = new ContentValues(); 59contentValues.put("name",ed_name.getText().toString()); 60contentValues.put("age",ed_age.getText().toString()); 61contentValues.put("sex",ed_sex.getText().toString()); 62db.update(contentValues,com.getName()); 63Toast.makeText(UpdateActivity.this,"修改成功",Toast.LENGTH_SHORT).show(); 64} 65//判断 66protected void onActivityResult(int requestCode, int resultCode, Intent data) { 67if(requestCode != 5){ 68 69} 70} 71 72@Override 73public void onClick(View v) { 74switch (v.getId()){ 75case R.id.but_queding: 76SqlUpdate(); 77break; 78case R.id.but_tianjia: 79SqlInsert(); 80break; 81} 82} 83 }

到这里所有功能就都写完了,我都写上注释了,如果有不明白的也可以留言,也可以加我Q,1171628833,本人刚安卓,小白一个,有同是小白的可以一起交流,共同成长,其他有一个Menu菜单,我没有写,因为不太明白,



 

    推荐阅读