NO IMAGE

1. MainActivity.java

   

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.sh.appsqlitedatabase"
          xmlns:android="http://schemas.android.com/apk/res/android">
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
2.xml檔案 
   
public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
Button bn;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//建立或開啟資料庫(要用絕路徑)
       db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()   "my.db3",null);
listView = (ListView)findViewById(R.id.show);
bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//獲取使用者的輸入
                String title = ((EditText)findViewById(R.id.title)).getText().toString();
String content = ((EditText)findViewById(R.id.content)).getText().toString();
try
                {
insertData(db,title,content);
Cursor cursor = db.rawQuery("select * from news_inf"
                                              ,null);
inflateList(cursor);
}
catch (Exception e)
{
//執行ddl建立資料表
                    db.execSQL("create table news_inf(_id integer"
                              " primary key autoincrement,"
                              " news_title varchar(50),"
                              " news_content varchar(255))" );
//執行insert語句
                    insertData(db,title,content);
//執行查詢
                    Cursor cursor = db.rawQuery("select * from news_inf"
                            ,null);
inflateList(cursor);
//                    e.printStackTrace();
                }
}
});
}
public  void insertData(SQLiteDatabase db,String title,String content)
{
//執行插入語句
        db.execSQL("insert into news_inf values (null,?,?)"
                ,new String[]{title,content});
}
public void  inflateList(Cursor cursor)
{
//         填充 SimpleCursorAdapter
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this,
R.layout.line,cursor,
new String[]{"news_title","news_content"},
new int[]{R.id.my_title,R.id.my_content},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//         顯示資料
               listView.setAdapter(adapter);
}
public void onDestroy()
{
super.onDestroy();
//退出程式時
      }
}
3.line.xml檔案 
  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"

              >
<TextView
           android:id="@ id/my_title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
<TextView
        android:id="@ id/my_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>