1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="欢迎进入用户管理系统"
/>
<Spinner
android:id="@+id/sp_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="@+id/ll_add"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入添加的学号:"
/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入添加的姓名:"
/>
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入添加的年龄:"
/>
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_del"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_delId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入删除的学号:"
/>
<Button
android:id="@+id/btn_del"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_select"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_selId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入查看的学号:"
/>
<Button
android:id="@+id/btn_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_update"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_upId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入修改用户的学号:"
/>
<EditText
android:id="@+id/et_upName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入修改的姓名:"
/>
<EditText
android:id="@+id/et_upAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入修改的年龄:"
/>
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
</LinearLayout>
2.DBHelper.java(数据库 表 创建类)
package com.example.day014_ex_stusys;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context){
super(context, "qf.db",null,1);
}
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
String sql="create table user(_id integer primary key,name text,age integer)";
arg0.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
3.java代码 MainActivity.java
package com.example.day014_ex_stusys;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
DBHelper helper;
SQLiteDatabase sd;
String data[]={"请选择:","增加学生信息","删除学生信息","修改学生信息","查看学生信息"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DBHelper(this);
sd=helper.getWritableDatabase();
Spinner lv = (Spinner) findViewById(R.id.sp_01);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data);
lv.setAdapter(adapter);
final LinearLayout ll_add=(LinearLayout) findViewById(R.id.ll_add);
final LinearLayout ll_del=(LinearLayout) findViewById(R.id.ll_del);
final LinearLayout ll_sel=(LinearLayout) findViewById(R.id.ll_select);
final LinearLayout ll_up=(LinearLayout) findViewById(R.id.ll_update);
final Button btn_add=(Button) findViewById(R.id.btn_add);
final Button btn_del=(Button) findViewById(R.id.btn_del);
final Button btn_sel=(Button) findViewById(R.id.btn_select);
final Button btn_up=(Button) findViewById(R.id.btn_update);
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg2==1){
ll_add.setVisibility(View.VISIBLE);
ll_sel.setVisibility(View.GONE);
ll_del.setVisibility(View.GONE);
ll_up.setVisibility(View.GONE);
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText id=(EditText) findViewById(R.id.et_id);
EditText name=(EditText) findViewById(R.id.et_name);
EditText age=(EditText) findViewById(R.id.et_age);
String ids=id.getText().toString();
String names=name.getText().toString();
String ages=age.getText().toString();
String sql="insert into user values('"+ids+"','"+names+"','"+ages+"')";
sd.execSQL(sql);
Toast.makeText(MainActivity.this,"插入成功!",0).show();
ll_add.setVisibility(View.GONE);
}
});
}//if
if(arg2==2){
ll_del.setVisibility(View.VISIBLE);
ll_add.setVisibility(View.GONE);
ll_sel.setVisibility(View.GONE);
ll_up.setVisibility(View.GONE);
btn_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText id=(EditText) findViewById(R.id.et_delId);
String ids=id.getText().toString();
String sql="delete from user where _id = '"+ids+"'";
sd.execSQL(sql);
Toast.makeText(MainActivity.this,"删除成功!",0).show();
ll_del.setVisibility(View.GONE);
}
});
}//if
if(arg2==3){
ll_add.setVisibility(View.GONE);
ll_sel.setVisibility(View.GONE);
ll_del.setVisibility(View.GONE);
ll_up.setVisibility(View.VISIBLE);
btn_up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText id=(EditText) findViewById(R.id.et_upId);
EditText name=(EditText) findViewById(R.id.et_upName);
EditText age=(EditText) findViewById(R.id.et_upAge);
String ids=id.getText().toString();
String names=name.getText().toString();
String ages=age.getText().toString();
String sql="update user set name='"+names+"',age='"+ages+"' where _id = '"+ids+"'";
sd.execSQL(sql);
Toast.makeText(MainActivity.this,"修改成功!",0).show();
ll_up.setVisibility(View.GONE);
}
});
}//if
if(arg2==4){
ll_sel.setVisibility(View.VISIBLE);
ll_add.setVisibility(View.GONE);
ll_del.setVisibility(View.GONE);
ll_up.setVisibility(View.GONE);
btn_sel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText id=(EditText) findViewById(R.id.et_selId);
String ids=id.getText().toString();
String sql="select * from user where _id = '"+ids+"'";
Cursor cs = sd.rawQuery(sql,null);
String name="",age="";
while(cs.moveToNext()){
name=cs.getString(cs.getColumnIndex("name"));
age=cs.getString(cs.getColumnIndex("age"));
}
Toast.makeText(MainActivity.this,"您所查的用户姓名:"+name+"&&&年龄为:"+age,Toast.LENGTH_LONG).show();
ll_sel.setVisibility(View.GONE);
}
});
}//if
}//method
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@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;
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章