直接用sql语句实现 ,添加、删除、修改
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{ Button btn1; Button btn2; Button btn3; EditText editText1; EditText editText2; SQLiteDatabase db; ListView listView1; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn3=(Button)findViewById(R.id.button3); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); editText1=(EditText)findViewById(R.id.editText1); editText2=(EditText)findViewById(R.id.editText2); listView1=(ListView)findViewById(R.id.listView1); try { File file= new File(getFilesDir().getCanonicalPath()+"/test.db"); if(file.exists()){ file.delete(); } //打开数据库,不存在则创建 db=SQLiteDatabase.openOrCreateDatabase(getFilesDir().getCanonicalPath()+"/test.db", null); String sql= "CREATE TABLE t_test ("+ " _id INTEGER NOT NULL,"+ " name TEXT,"+ " PRIMARY KEY (_id ASC)"+ " )"; db.execSQL(sql); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public void onClick(View paramView) { if(paramView==btn1){ int _id=Integer.parseInt(editText1.getText().toString()); String name=editText2.getText().toString(); String sql="select * from t_test where _id="+_id; Cursor cursor=db.rawQuery(sql, null); if(cursor.getCount()>0){ Toast.makeText(MainActivity.this, "记录已存在", Toast.LENGTH_SHORT).show(); }else{ sql="INSERT INTO t_test VALUES (?, ?)"; db.execSQL(sql, new Object[]{_id,name}); } }else if(paramView==btn2){ int _id=Integer.parseInt(editText1.getText().toString()); String name=editText2.getText().toString(); String sql="update t_test set _id=?,name=? where _id=?"; db.execSQL(sql, new Object[]{_id,name,_id}); }else if(paramView==btn3){ int _id=Integer.parseInt(editText1.getText().toString()); String sql="delete from t_test where _id=?"; db.execSQL(sql, new Object[]{_id}); editText1.setText(""); editText2.setText(""); } cursor=db.rawQuery("select * from t_test", null); SimpleCursorAdapter adapter=new SimpleCursorAdapter(MainActivity.this, R.layout.item, cursor, new String[]{"_id","name"}, new int[]{R.id.textView2,R.id.textView4}); listView1.setAdapter(adapter); listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) { if(cursor.moveToPosition(paramInt)){ editText1.setText(""+cursor.getInt(cursor.getColumnIndex("_id"))); editText2.setText(cursor.getString(cursor.getColumnIndex("name"))); } } }); } }
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:text="ID:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="33dp" android:layout_toRightOf="@+id/textView1" android:text="001" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_marginLeft="17dp" android:layout_toRightOf="@+id/textView2" android:text="名字:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_marginLeft="27dp" android:layout_toRightOf="@+id/textView3" android:text="测试1" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
activity_main.xml
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_centerHorizontal="true" android:text="修改" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="21dp" android:text="删除" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_marginTop="11dp" android:text="添加" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/button1" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:ems="10" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/editText1" android:layout_marginTop="19dp" android:text="名字" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_alignParentTop="true" android:layout_marginTop="21dp" android:layout_toLeftOf="@+id/editText1" android:text="id" android:textAppearance="?android:attr/textAppearanceMedium" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/button2" > </ListView> </RelativeLayout>
时间: 2024-11-09 14:34:18