首页 > 代码库 > Android高级-Android操作SQL数据管理,增删改查
Android高级-Android操作SQL数据管理,增删改查
已经学了好几天SQL了,昨天刚接触到Android操作SQL数据库,晚上老师留了一个作业,效果图如下
分别是,主界面,和修改,添加,以及删除界面
首先我们先来实现布局 MainActivity.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 4 android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" 5 6 tools:context="wuxianedu.com.sqllistview.MainActivity"> 7 8 <ListView android:id="@+id/lv_liebiao" 9 android:layout_width="match_parent"10 android:layout_height="match_parent">11 </ListView>12 </RelativeLayout>
布局很简洁,只有一个ListView,
下面是List列表的item布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <LinearLayout android:layout_width="0dp" 6 android:layout_height="wrap_content" 7 android:layout_weight="5"> 8 <TextView android:id="@+id/tv_name" 9 android:layout_width="0dp" android:layout_gravity="center_vertical"10 android:layout_height="wrap_content"11 android:text="aaa" android:layout_weight="1"12 android:gravity="center"/>13 14 <TextView android:id="@+id/tv_age"15 android:layout_width="wrap_content"16 android:layout_height="wrap_content" android:layout_gravity="center_vertical"17 android:text="aaa" android:layout_weight="1"18 android:gravity="center"/>19 <TextView android:id="@+id/tv_sex"20 android:layout_width="wrap_content"21 android:layout_height="wrap_content" android:layout_gravity="center_vertical"22 android:text="aaa" android:layout_weight="1"23 android:gravity="center"/>24 </LinearLayout>25 <Button android:id="@+id/but_name"26 android:layout_width="0dp"27 android:layout_height="wrap_content"28 android:text="修改" android:layout_weight="1"29 android:gravity="center"/>30 31 <Button android:id="@+id/but_delete"32 android:layout_width="0dp"33 android:layout_height="wrap_content"34 android:text="删除" android:layout_weight="1"35 android:gravity="center"/>36 </LinearLayout>
之后是修改和添加界面的XML代码
UpdateActivity.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:orientation="vertical" 6 android:layout_height="match_parent" 7 android:paddingBottom="@dimen/activity_vertical_margin" 8 android:paddingLeft="@dimen/activity_horizontal_margin" 9 android:paddingRight="@dimen/activity_horizontal_margin"10 android:paddingTop="@dimen/activity_vertical_margin"11 tools:context="wuxianedu.com.sqllistview.UpdateActivity">12 13 <EditText android:id="@+id/ed_name"14 android:layout_width="match_parent"15 android:layout_height="wrap_content"16 android:hint="请输入姓名"/>17 18 <EditText android:id="@+id/ed_age"19 android:layout_width="match_parent"20 android:layout_height="wrap_content"21 android:hint="请输入年龄"22 android:layout_below="@+id/ed_name"/>23 24 <EditText android:id="@+id/ed_sex"25 android:layout_width="match_parent"26 android:layout_height="wrap_content"27 android:hint="请输入性别"28 android:layout_below="@+id/ed_age"/>29 30 <Button android:id="@+id/but_queding"31 android:layout_width="match_parent"32 android:layout_height="wrap_content"33 android:hint="修改用户"34 android:layout_below="@+id/ed_sex"/>35 36 <Button android:id="@+id/but_tianjia"37 android:layout_width="match_parent"38 android:layout_height="wrap_content"39 android:hint="添加用户"40 android: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 18 private MainAdapter mainAdapter;//构建数据源19 private List list;//接收数据的LIST20 private DBauxiliary db;//数据库操作类的实例21 22 @Override23 protected void onCreate(Bundle savedInstanceState) {24 super.onCreate(savedInstanceState);25 setContentView(R.layout.activity_main);26 ListView listView = (ListView) findViewById(R.id.lv_liebiao);27 db = DBauxiliary.getInstance(this);28 list= db.select();29 //构建数据源30 mainAdapter = new MainAdapter(this,list);31 //添加数据32 listView.setAdapter(mainAdapter);33 }34 //Activity创建或者从被覆盖、后台重新回到前台时被调用 35 @Override36 protected void onResume() {37 super.onResume();38 //重新获取list数据39 list = db.select();40 //给list赋值 更新41 mainAdapter.setList(list);42 }43 44 @Override //菜单的点击事件45 public boolean onCreateOptionsMenu(Menu menu) {46 getMenuInflater().inflate(R.menu.menu,menu);47 menu.findItem(R.id.add).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {48 @Override49 public boolean onMenuItemClick(MenuItem item) {50 //点击添加跳转51 Intent intent=new Intent(MainActivity.this,UpdateActivity.class);52 startActivityForResult(intent,5);53 return false;54 }55 });56 return 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 { 23 private Context context; 24 private List<JavaBean> list;//接收数据的LIST 25 private DBauxiliary db;//数据库操作类的实例 26 //适配器 27 public MainAdapter(Context context,List<JavaBean> list){ 28 this.context = context; 29 this.list = list; 30 } 31 //更新集合用 32 public void setList(List<JavaBean> list) { 33 this.list = list; 34 notifyDataSetChanged(); 35 } 36 37 @Override 38 public int getCount() { 39 return list.size(); 40 } 41 42 @Override 43 public Object getItem(int position) { 44 return list.get(position); 45 } 46 47 @Override 48 public long getItemId(int position) { 49 return position; 50 } 51 52 @Override 53 public View getView(final int position, View convertView, ViewGroup parent) { 54 final HUi hui; 55 if(convertView == null){ 56 hui = new HUi(); 57 final int wo ; 58 wo = position; 59 db = DBauxiliary.getInstance(context); 60 convertView = LayoutInflater.from(context).inflate(R.layout.item_lie, null); 61 hui.name = (TextView) convertView.findViewById(R.id.tv_name); 62 hui.age = (TextView) convertView.findViewById(R.id.tv_age); 63 hui.sex = (TextView) convertView.findViewById(R.id.tv_sex); 64 hui.update = (Button) convertView.findViewById(R.id.but_name); 65 hui.delete = (Button) convertView.findViewById(R.id.but_delete); 66 67 // 68 //修改按钮 69 hui.update.setOnClickListener(new View.OnClickListener() { 70 @Override 71 public void onClick(View v) { 72 Intent intent = new Intent(context,UpdateActivity.class); 73 int on = (int) hui.update.getTag(); 74 JavaBean javabean = list.get(on); 75 intent.putExtra("name",javabean); 76 context.startActivity(intent); 77 78 } 79 }); 80 convertView.setTag(hui); 81 }else{ 82 hui = (HUi) convertView.getTag(); 83 } 84 85 final JavaBean java = list.get(position); 86 hui.update.setTag(position); 87 hui.name.setText(java.getName()); 88 hui.age.setText(String.valueOf(java.getAge())); 89 hui.sex.setText(java.getSex()); 90 final String naaa = java.getName(); 91 //构建删除对话框 92 hui.delete.setOnClickListener(new View.OnClickListener() { 93 @Override 94 public void onClick(View v) { 95 AlertDialog.Builder builder = new AlertDialog.Builder(context); 96 builder.setMessage("确定删除吗?"); 97 builder.setPositiveButton("取消",null); 98 builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { 99 @Override100 public void onClick(DialogInterface dialog, int which) {101 db.delete(naaa);102 list = db.select();103 setList(list);104 notifyDataSetChanged();105 Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();106 }107 }).show();108 109 }110 });111 return convertView;112 }113 114 class HUi {115 TextView name,age,sex;116 Button 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 16 private DBauxiliary db;17 private EditText ed_name,ed_sex,ed_age;18 private JavaBean com;19 private Button xiugai,shanchu;20 21 @Override22 protected void onCreate(Bundle savedInstanceState) {23 super.onCreate(savedInstanceState);24 setContentView(R.layout.activity_update);25 ed_name= (EditText) findViewById(R.id.ed_name);26 ed_age = (EditText) findViewById(R.id.ed_age);27 ed_sex = (EditText) findViewById(R.id.ed_sex);28 xiugai = (Button) findViewById(R.id.but_tianjia);29 shanchu = (Button) findViewById(R.id.but_queding);30 Intent on = getIntent();31 com = (JavaBean) on.getSerializableExtra("name");32 if(com == null){33 shanchu.setVisibility(View.GONE);34 }else{35 xiugai.setVisibility(View.GONE);36 ed_name.setText(com.getName()+"");37 ed_age.setText(String.valueOf(com.getAge())+"");38 ed_sex.setText(com.getSex()+"");39 }40 db = DBauxiliary.getInstance(UpdateActivity.this);41 /* ed_name.setText(com.getName()+"");42 ed_age.setText(String.valueOf(com.getAge())+"");43 ed_sex.setText(com.getSex()+"");*/44 xiugai.setOnClickListener(this);45 shanchu.setOnClickListener(this);46 47 }48 private void SqlInsert(){49 ContentValues contentValues = new ContentValues();50 contentValues.put("name",ed_name.getText().toString());51 contentValues.put("age",ed_age.getText().toString());52 contentValues.put("sex",ed_sex.getText().toString());53 db.insert(contentValues);54 Toast.makeText(UpdateActivity.this,"添加成功",Toast.LENGTH_SHORT).show();55 }56 //删除数据57 private void SqlUpdate(){58 ContentValues contentValues = new ContentValues();59 contentValues.put("name",ed_name.getText().toString());60 contentValues.put("age",ed_age.getText().toString());61 contentValues.put("sex",ed_sex.getText().toString());62 db.update(contentValues,com.getName());63 Toast.makeText(UpdateActivity.this,"修改成功",Toast.LENGTH_SHORT).show();64 }65 //判断66 protected void onActivityResult(int requestCode, int resultCode, Intent data) {67 if(requestCode != 5){68 69 }70 }71 72 @Override73 public void onClick(View v) {74 switch (v.getId()){75 case R.id.but_queding:76 SqlUpdate();77 break;78 case R.id.but_tianjia:79 SqlInsert();80 break;81 }82 }83 }
到这里所有功能就都写完了,我都写上注释了,如果有不明白的也可以留言,也可以加我Q,1171628833,本人刚安卓,小白一个,有同是小白的可以一起交流,共同成长,其他有一个Menu菜单,我没有写,因为不太明白,
Android高级-Android操作SQL数据管理,增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。