androidspinner动态添加数据,通过spinner简单动态实现⽤户管
理系统的增删。。。
1.布局⽂件
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:layout_width="wrap_content"
android:text="欢迎进⼊⽤户管理系统"
/>
android:id="@+id/sp_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/ll_add"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输⼊添加的学号:"
/>
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输⼊添加的姓名:"
/>
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="请输⼊添加的年龄:"
/>
android:id="@+id/btn_add"
android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交"
/>
android:id="@+id/ll_del"
android:visibility="gone"
android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"
>
android:id="@+id/et_delId"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输⼊删除的学号:"
/>
android:id="@+id/btn_del"
android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交"
/>
android:id="@+id/ll_select"
android:visibility="gone"
android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"
>
android:id="@+id/et_selId"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输⼊查看的学号:"
/>
android:id="@+id/btn_select"
android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交"
/>
android:id="@+id/ll_update"
android:visibility="gone"
android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"
>
android:id="@+id/et_upId"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输⼊修改⽤户的学号:"
/>
android:id="@+id/et_upName"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输⼊修改的姓名:"
/>
android:id="@+id/et_upAge"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输⼊修改的年龄:"
/>
android:id="@+id/btn_update"
android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交"
/>
2.DBHelper.java(数据库 表 创建类) ample.day014_ex_stusys;
t.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)"; SQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub
}
}
3.java代码 MainActivity.java
ample.day014_ex_stusys;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
jfinal增删改查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) {
setContentView(R.layout.activity_main);
helper=new DBHelper(this);
WritableDatabase();
Spinner lv = (Spinner) findViewById(R.id.sp_01);
ArrayAdapteradapter=new ArrayAdapter(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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论