package practise.lxm.hello; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteException; import android.os.Environment; 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.SimpleCursorAdapter; import java.io.File; import java.io.IOException; public class MainActivity extends Activity { sqliteDatabase db; EditText edTxtTitle; EditText edTxtContent; ListView lvNews; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edTxtTitle = (EditText)findViewById(R.id.edTxt_title); edTxtContent = (EditText)findViewById(R.id.edTxt_content); lvNews = (ListView)findViewById(R.id.lv_test); Button btn = (Button)findViewById(R.id.btn_commit); //打开或创建数据库 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ try { File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "//test.db3"); db = sqliteDatabase.openOrCreateDatabase(file,null); } catch (IOException e) { e.printStackTrace(); } } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { insert(db,edTxtTitle.getText().toString(),edTxtContent.getText().toString()); //插入 }catch (sqliteException sql){ //失败创建表 String strsql = "create table news(_id integer primary key autoincrement,title nvarchar(50) not null,content nvarchar(100) not null)"; db.execsql(strsql); //重新插入 insert(db,edTxtContent.getText().toString()); } //刷新列表 String strsql = "select * from news"; Cursor cursor= db.rawQuery(strsql,null); //查询 inflateList(cursor); } }); } //插入信息 private void insert(sqliteDatabase db,String title,String content){ String strInsert = "insert into news(title,content) values(?,?)"; db.execsql(strInsert,new String[]{title,content}); } //显示数据到列表 private void inflateList(Cursor cursor){ //表中必须含有名为"_id"的列,该列数据类型等无所谓 SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.lv_file_item,cursor,new String[]{"title","content"},new int[]{R.id.tv_title,R.id.tv_content},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lvNews.setAdapter(simpleCursorAdapter); } protected void onDestroy() { if(db != null) { //关闭数据库连接 db.close(); } super.onDestroy(); } }
lv_file_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="20sp" android:id="@+id/tv_title"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="15sp" android:id="@+id/tv_content"/> </LinearLayout>
activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1" android:padding="5pt"> <TableRow android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edTxt_title"/> </TableRow> <TableRow android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edTxt_content"/> </TableRow> <Button android:text="提交" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_commit"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_test"></ListView> </TableLayout>