----Main.java
publicclassMainextendsActivity{ sqliteDatabasedb; ListViewlistView; EditTexteditText1,editText2;//要添加的标题和context Buttonbutton; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(ListView)findViewById(R.id.listView1); editText1=(EditText)findViewById(R.id.editText1); editText2=(EditText)findViewById(R.id.editText2); button=(Button)findViewById(R.id.button1); ///data/data/com.example.dbtest/files--/my.db3 db=sqliteDatabase.openOrCreateDatabase(this.getFilesDir().toString() +"/my.db3",null); button.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ Stringstr1=editText1.getText().toString(); Stringstr2=editText2.getText().toString(); try{ insertToDB(db,str1,str2); Cursorcursor=db.rawQuery("select*fromnews_info",null); inflateListView(cursor); }catch(sqliteExceptione){ db.execsql("createtablenews_info(_idintegerprimarykeyautoincrement," +"news_titlevarchar(50)," +"news_contentvarchar(255))"); insertToDB(db,str2); Cursorcursor=db.rawQuery("selecte*fromnews_info",null); inflateListView(cursor); } } }); } privatevoidinsertToDB(sqliteDatabasedb,Stringstr1,Stringstr2){ db.execsql("insertintonews_infovalues(null,?,?)",newString[]{ str1,str2}); } privatevoidinflateListView(Cursorcursor){ SimpleCursorAdapteradapter=newSimpleCursorAdapter(Main.this,R.layout.item,cursor,newString[]{"news_title","news_content"},newint[]{R.id.textView1,R.id.textView2},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); } @Override protectedvoidonDestroy(){ super.onDestroy(); if(db!=null&&db.isOpen()){ db.close(); } } }
SimpleCursorAdapter封装Cursor时,要求数据表的主键列的列名为 _id 。因为SimpleCursorAdapter只能识别 列名为
_id的主键。否则会报错。
同java中的操作JDBC一样,数据库最后也要关闭 db.close(); 来回收资源。
---main.xml
<RelativeLayoutxmlns: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}"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:ems="10"> <requestFocus/> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" android:ems="10"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText2" android:text="插入数据"/> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1"> </ListView> </RelativeLayout>
listeview 每个子项的布局文件 item.xml:
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="TextView"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/textView1" android:text="TextView"/> </RelativeLayout>
运行效果:
原文链接:https://www.f2er.com/sqlite/200019.html