麻烦帮我看下哪里写的有问题,程序运行起来不能达到预期的功能
package com.xudongting.databasedemo; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.SimpleAdapter; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import java.io.File; public class MainActivity extends AppCompatActivity { private EditText name, age, _id; private RadioGroup sex; private RadioButton male, female; private Button btn_add, btn_search, btn_delete, btn_modify; private String sex_inchoice="男"; private String s_name,s_age,s_id; private ListView listView; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId==R.id.male){ sex_inchoice="男"; }else sex_inchoice="女"; } }); String dataBaseName = Environment.getExternalStorageDirectory()+"/stu.db"; SQLiteOpenHelper helper = new SQLiteOpenHelper(this, dataBaseName, null, 1) { @Override public void onCreate(SQLiteDatabase db) { String sql="create table info_db( _id integer primary key autoincrement,name varchar(20),age varchar(4), sex varchar(4) )"; db.execSQL(sql); Toast.makeText(MainActivity.this, "数据库创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Toast.makeText(MainActivity.this, "数据库升级成功", Toast.LENGTH_SHORT).show(); } }; db = helper.getReadableDatabase(); } private void initView() { name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); _id = (EditText) findViewById(R.id._id); sex = (RadioGroup) findViewById(R.id.sex); male = (RadioButton) findViewById(R.id.male); female = (RadioButton) findViewById(R.id.female); btn_add = (Button) findViewById(R.id.btn_add); btn_search = (Button) findViewById(R.id.btn_search); btn_delete = (Button) findViewById(R.id.btn_delete); btn_modify = (Button) findViewById(R.id.btn_modif); listView=(ListView)findViewById(R.id.listView); s_name=name.getText().toString(); s_age=age.getText().toString(); s_id=_id.getText().toString(); } public void onClick(View v) { switch (v.getId()) { case R.id.btn_add: String sql1="insert into info_db(name,age,sex) values(?,?,?)"; db.execSQL(sql1,new String[]{s_name,s_age,sex_inchoice}); Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show(); break; case R.id.btn_search: String sql2="select * from info_db"; Cursor c=db.rawQuery(sql2,null); SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.item,c,new String[]{"_id","name","age","sex"},new int[]{R.id.item_1,R.id.item_2,R.id.item_3,R.id.item_4}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); break; case R.id.btn_delete: String sql3="delete from info_db where _id=?"; db.execSQL(sql3,new String[]{s_id}); Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show(); break; case R.id.btn_modif: String sql4="update info_db set name=?,age=?,sex=? where _id=?"; db.execSQL(sql4,new String[]{s_name,s_age,sex_inchoice,s_id}); Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show(); break; } } }
13
收起
正在回答 回答被采纳积分+1
3回答
慕粉2144182527
2018-03-16 20:03:26
虚拟机运行起来的效果图,而且删除和修改按钮用不了,我的as里面的android device monitor也打不开,想看下数据库表是否创建成功也查看不了
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星