SQLite的基本操作在前面已经都示例了。这里是前面示例的进阶版本。在活动间数据的互通。这个示例真的让我无语,一个是表的名字没对应还有一个是少了一个等于号浪费了我很多事件,果然自己看自己代码是很难看出错误的,话不多说上代码:
这次为了更直观的看活动间的跳转和数据交互,所以建4个Activity
见了一个MyHelper继承SQLiteOpenHelper然后在其他的Activity调用。敲黑板!!!
直接在其他Activity新建然后就直接用。
MyHelper.java:
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context){
super(context,"sss.db",null,1);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT ,name VARCHAR(20),xb VARCHAR(20),dz VARCHAR(20),dh VARCHAR(20),qq VARCHAR(20),em VARCHAR(20))");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
}
}
前面说了MyHelpe继承SQLiteOpenHelper并重写onCreate和onUpgrade
CX.java:
package com.example.myapplication;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CX extends AppCompatActivity {
private SQLiteDatabase db;
private ContentValues values;
private TextView tv_cx;
MyHelper myHelper=new MyHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cx);
//myHelper=new MyHelper(this);
db=myHelper.getReadableDatabase();
tv_cx=findViewById(R.id.tv_cx);
Cursor cursor=db.query("information",null,null,null,null,null,null);
if (cursor.getCount()==0)
{
tv_cx.setText("");
Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
}else
{
cursor.moveToFirst();
tv_cx.setText("Name:"+cursor.getString(1)+"\n"+"SEX:"+cursor.getString(2)+"\n"+"地址:"+cursor.getString(3)+"\n"+"电话:"+cursor.getString(4)+"\n"+"QQ:"+cursor.getString(5)+"\n"+"Email:"+cursor.getString(6)+"\n");
}
while (cursor.moveToNext())
{
tv_cx.append("Name:"+cursor.getString(1)+"\n"+";SEX:"+cursor.getString(2)+"\n"+"地址:"+cursor.getString(3)+"\n"+"电话:"+cursor.getString(4)+"\n"+"QQ:"+cursor.getString(5)+"\n"+"Email:"+cursor.getString(6)+"\n");
}
cursor.close();
db.close();
}
}
XG.java:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class XG extends AppCompatActivity implements View.OnClickListener {
MyHelper myHelper=new MyHelper(this);
private Button qx;
private Button qr;
private EditText xm;
private RadioGroup xb;
private EditText dz;
private EditText dh;
private EditText QQ;
private EditText Email;
private RadioButton nan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xg);
init();//初始化控件
}
public void init()
{
qx=findViewById(R.id.qx);
qr=findViewById(R.id.qr);
qx.setOnClickListener(this);
qr.setOnClickListener(this);
xm=findViewById(R.id.xm);
xb=findViewById(R.id.xb);
dz=findViewById(R.id.dz);
dh=findViewById(R.id.dh);
QQ=findViewById(R.id.qq);
Email=findViewById(R.id.em);
nan=findViewById(R.id.xb_na);
}
public void onClick(View v)
{
String xg_xm;
String xg_xb;
String xg_dz;
String xg_dh;
String xg_qq;
String xg_em;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()){
case R.id.qx:
finish() ;
break;
case R.id.qr:
db=myHelper.getWritableDatabase();
values=new ContentValues();
values.put("xb",xg_xb=nan.isChecked()? "男":"女");
values.put("dz",xg_dz=dz.getText().toString());
values.put("dh",xg_dh=dh.getText().toString());
values.put("qq",xg_qq=QQ.getText().toString());
values.put("em",xg_em=Email.getText().toString());
db.update("information",values,"name=?",new String[]{xm.getText().toString()});
Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
}
TJ.java:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class TJ extends AppCompatActivity implements View.OnClickListener {
MyHelper myHelper=new MyHelper(this);
private Button qx;
private Button qr;
private EditText xm;
private RadioGroup xb;
private EditText dz;
private EditText dh;
private EditText QQ;
private EditText Email;
private RadioButton nan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tj);
init();//初始化控件
}
public void init()
{
qx=findViewById(R.id.qx);
qr=findViewById(R.id.qr);
qx.setOnClickListener(this);
qr.setOnClickListener(this);
xm=findViewById(R.id.xm);
xb=findViewById(R.id.xb);
dz=findViewById(R.id.dz);
dh=findViewById(R.id.dh);
QQ=findViewById(R.id.qq);
Email=findViewById(R.id.em);
nan=findViewById(R.id.xb_na);
}
public void onClick(View v)
{
String tj_xm;
String tj_xb;
String tj_dz;
String tj_dh;
String tj_qq;
String tj_em;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()){
case R.id.qx:
finish() ;
break;
case R.id.qr:
tj_xm=xm.getText().toString();
tj_xb=nan.isChecked()? "男":"女";
tj_dz=dz.getText().toString();
tj_dh=dh.getText().toString();
tj_qq=QQ.getText().toString();
tj_em=Email.getText().toString();
db=myHelper.getWritableDatabase();
values=new ContentValues();
values.put("name",tj_xm);
values.put("xb",tj_xb);
values.put("dz",tj_dz);
values.put("dh",tj_dh);
values.put("qq",tj_qq);
values.put("em",tj_em);
db.insert("information",null,values);
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
}
MainActivity:
package com.example.myapplication;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button tj;
private Button cz;
private Button sc;
private Button xg;
private SQLiteDatabase db;
MyHelper myHelper=new MyHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tj=findViewById(R.id.btn_tj);
cz=findViewById(R.id.btn_cz);
sc=findViewById(R.id.btn_sc);
xg=findViewById(R.id.btn_xg);
tj.setOnClickListener(this);
cz.setOnClickListener(this);
sc.setOnClickListener(this);
xg.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId()){
case R.id.btn_tj:
Intent intent1=new Intent(MainActivity.this,TJ.class);
startActivity(intent1);
break;
case R.id.btn_cz:
Intent intent2=new Intent(MainActivity.this,CX.class);
startActivity(intent2);
break;
case R.id.btn_xg:
Intent intent3=new Intent(MainActivity.this,XG.class);
startActivity(intent3);
break;
case R.id.btn_sc:
db=myHelper.getReadableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
}
注意哈:
这里一开始我没怎么注意后来输入数据多了发现呵呵,这里还得加滚动ScrollView和HorizontalScrollView,在前面有介绍。