git diff命令的使用
阿新 • • 發佈:2020-12-04
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.iwanghang.sqlitedemo"> <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> <!-- 插入對話方塊 --> <activity android:name="com.iwanghang.sqlitedemo.dialog.InsertDialog" android:theme="@android:style/Theme.Dialog" > </activity> <!-- 刪除對話方塊 --> <activity android:name="com.iwanghang.sqlitedemo.dialog.DeleteDialog" android:theme="@android:style/Theme.Dialog" > </activity> <!-- 更新對話方塊 --> <activity android:name="com.iwanghang.sqlitedemo.dialog.UpdateDialog" android:theme="@android:style/Theme.Dialog" > </activity> </application>
package com.iwanghang.sqlitedemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.iwanghang.sqlitedemo.dialog.DeleteDialog; import com.iwanghang.sqlitedemo.dialog.InsertDialog; import com.iwanghang.sqlitedemo.dialog.UpdateDialog; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements View.OnClickListener { private SQLiteHelper db; private ListAdapter listAdapter; /** * 插入請求程式碼 */ private static final int INSERT_REQUESTCODE = 1; /** * 插入結果程式碼 */ private static final int INSERT_RESULTCODE = 1; /** * 刪除請求程式碼 */ private static final int DELETE_REQUESTCODE = 2; /** * 刪除結果程式碼 */ private static final int DELETE_RESULTCODE = 2; /** * 修改請求程式碼 */ private static final int UPDATE_REQUESTCODE = 3; /** * 修改結果程式碼 */ private static final int UPDATE_RESULTCODE = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new SQLiteHelper(this, "person.db", null, 1); init(); } private void init() { findViewById(R.id.btn_insert).setOnClickListener(this); findViewById(R.id.btn_delete).setOnClickListener(this); findViewById(R.id.btn_update).setOnClickListener(this); findViewById(R.id.btn_query).setOnClickListener(this); listAdapter = new ListAdapter(new ArrayList<Person>(), this); findViewById(R.id.list_data, ListView.class).setAdapter(listAdapter); } private <T> T findViewById(int id, Class<T> c) { return (T) findViewById(id); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_insert: // 插入 insert(); break; case R.id.btn_delete: // 刪除 delete(); break; case R.id.btn_update: // 修改 update(); break; case R.id.btn_query: // 查詢 query(); break; } } /** * 新增 */ private void insert() { Intent intent = new Intent(this, InsertDialog.class); startActivityForResult(intent, INSERT_REQUESTCODE); } /** * 刪除 */ private void delete() { Intent intent = new Intent(this, DeleteDialog.class); startActivityForResult(intent, DELETE_REQUESTCODE); } /** * 更新 */ private void update() { Intent intent = new Intent(this, UpdateDialog.class); startActivityForResult(intent, UPDATE_REQUESTCODE); } /** * 查詢 */ private void query() { List<Person> list = db.queryAllPerson(); // 查詢所有的Person loadData(list); // 載入資料到ListView上面 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case INSERT_REQUESTCODE: if (resultCode == INSERT_RESULTCODE) { // 插入請求 if (data != null) { db.addPerson((Person) data.getSerializableExtra("person")); showMessage("插入成功!"); query(); } else { showMessage("取消插入!"); } } break; case DELETE_REQUESTCODE: if (resultCode == DELETE_RESULTCODE) { // 刪除請求 if (data != null) { int _id = data.getIntExtra("_id", -1); if (_id != -1) { if (db.queryPersonById(_id) != null) { db.deletePerson(_id); showMessage("刪除成功!"); query(); } else { showMessage("刪除失敗\t_id:" + _id + "不存在!"); } } else { showMessage("刪除失敗!"); } } else { showMessage("取消刪除!"); } } break; case UPDATE_REQUESTCODE: if (resultCode == UPDATE_RESULTCODE) { // 修改請求 if (data != null) { Person person = (Person) data .getSerializableExtra("person"); int _id = person.get_id(); if (db.queryPersonById(person.get_id()) != null) { db.updatePerson(person); showMessage("修改成功!"); query(); } else { showMessage("修改失敗\t_id:" + _id + "不存在!"); } } else { showMessage("取消修改!"); } } break; } } /** * 顯示訊息 * * @param msg * 訊息 */ private void showMessage(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } /** * 載入資料到ListView * * @param list * Person集合 */ private void loadData(List<Person> list) { listAdapter.setList(list); listAdapter.notifyDataSetChanged(); // 重新整理資料 }
package com.iwanghang.sqlitedemo; import java.io.Serializable; /** * 人 */ public class Person implements Serializable { /** * 序列化的版本號 */ private static final long serialVersionUID = 1L; /** * 元件 */ private int _id; /** * 姓名 */ private String name; /** * 年齡 */ private int age; /** * 性別 */ private String sex; /** * set方法 * get方法 */ public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }
package com.iwanghang.sqlitedemo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class ListAdapter extends BaseAdapter { private List<Person> list; private LayoutInflater inflater; public ListAdapter(List<Person> list, Context context) { this.list = list; this.inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void setList(List<Person> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = inflater.inflate(R.layout.activity_main_list1, null); TextView idView = (TextView) convertView.findViewById(R.id.text_id); TextView nameView = (TextView) convertView.findViewById(R.id.text_name); TextView ageView = (TextView) convertView.findViewById(R.id.text_age); TextView sexView = (TextView) convertView.findViewById(R.id.text_sex); Person person = list.get(position); idView.setText(String.valueOf(person.get_id())); nameView.setText(person.getName()); ageView.setText(String.valueOf(person.getAge())); sexView.setText(String.valueOf(person.getSex())); return convertView; } }
package com.iwanghang.sqlitedemo; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * 資料庫建立、更新 */ public class SQLiteHelper extends SQLiteOpenHelper { /** * @param context * 上下文 * @param name * 資料庫名稱 * @param factory * 遊標工廠 * @param version * 資料庫版本 */ public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } // 建立資料庫 @Override public void onCreate(SQLiteDatabase db) { Log.e("SqliteHelper", "資料庫建立"); String sql = "create table person(_id integer Primary Key autoincrement,name varchar(20), age integer,sex varchar(20))"; db.execSQL(sql); } // 資料庫更新 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.e("SqliteHelper", "資料庫更新"); } /** * 新增Person到資料庫 * * @param person * Person */ public void addPerson(Person person) { Log.e("SqliteHelper", "插入"); SQLiteDatabase db = getWritableDatabase(); // 以讀寫的形式開啟資料庫 // db.execSQL("insert into person(name,age) values(" // + String.format("'%s'", person.getName()) + "," // + person.getAge() + ");"); // 插入資料庫 // insert into person(name,age,sex) values('liudehua',50,'man') // db.execSQL( // "insert into person(name,age,sex) values(" // + String.format("'%s'", person.getName()) + "," // + person.getAge() + "," // + String.format("'%s'", person.getSex()) + // ");" // ); // 插入資料庫 db.execSQL( "insert into person(name,age,sex) values(" + String.format("'%s'", person.getName()) + "," + person.getAge() + "," + String.format("'%s'", person.getSex()) + ");" ); // 插入資料庫 db.close(); // 關閉資料庫連線 } /** * 更新Person * * @param person * Person */ public void updatePerson(Person person) { Log.e("SqliteHelper", "更新"); SQLiteDatabase db = getWritableDatabase(); // 以讀寫的形式開啟資料庫 // String sql = "update person set name=" // + String.format("'%s'", person.getName()) + ",age=" // + person.getAge() + " where _id=" + person.get_id(); String sql = "update person set name=" + String.format("'%s'", person.getName()) + ",age=" + person.getAge() + ",sex=" + String.format("'%s'", person.getSex()) + " where _id=" + person.get_id(); Log.e("updatePerson", sql); db.execSQL(sql); // 更新資料庫 db.close(); // 關閉資料庫連線 } /** * 刪除Person * * @param _id * Person的id */ public void deletePerson(int _id) { Log.e("SqliteHelper", "刪除"); SQLiteDatabase db = getWritableDatabase(); // 以讀寫的形式開啟資料庫 String sql = "_id = ?"; String wheres[] = { String.valueOf(_id) }; db.delete("person", sql, wheres); // 資料庫刪除 db.close(); // 關閉資料庫 } /** * 查詢所有的Person * * @return 所有Person集合 */ public List<Person> queryAllPerson() { List<Person> list = new ArrayList<Person>(); SQLiteDatabase db = getReadableDatabase(); // 以只讀的方式開啟資料庫 String sql = "select * from person;"; Cursor cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); String sex = cursor.getString(cursor.getColumnIndex("sex")); Person person = new Person(); person.set_id(_id); person.setName(name); person.setAge(age); person.setSex(sex); System.out.println(" ---- sex = " + sex); list.add(person); // 新增到陣列 } cursor.close(); // 關閉遊標 db.close(); // 關閉資料庫 return list; } /** * 根據id查詢Person * * @param _id * id * @return Person */ public Person queryPersonById(int _id) { Person person = null; SQLiteDatabase db = getReadableDatabase(); // 以只讀方式開啟資料庫 // String[] columns = { "_id", "name", "age" }; String[] columns = { "_id", "name", "age", "sex" }; String selection = "_id=?"; String[] selectionArgs = { String.valueOf(_id) }; Cursor cursor = db.query("person", columns, selection, selectionArgs, null, null, null); if (cursor.moveToNext()) { person = new Person(); person.set_id(cursor.getInt(cursor.getColumnIndex("_id"))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"))); person.setName(cursor.getString(cursor.getColumnIndex("name"))); person.setName(cursor.getString(cursor.getColumnIndex("sex"))); } return person; } }
package com.iwanghang.sqlitedemo.dialog; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.EditText; import com.iwanghang.sqlitedemo.Person; import com.iwanghang.sqlitedemo.R; /** * 插入對話方塊 */ public class InsertDialog extends Activity implements View.OnClickListener { /** * 插入請求程式碼 大於0 */ private static final int INSERT_REQUESTCODE = 1; /** * 姓名控制元件 */ private EditText nameView; /** * 年齡控制元件 */ private EditText ageView; /** * 性別控制元件 */ private EditText sexView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉標題欄 setContentView(R.layout.insert_dialog); init(); } private void init() { findViewById(R.id.insert_confirm).setOnClickListener(this); findViewById(R.id.insert_cancel).setOnClickListener(this); nameView = (EditText) findViewById(R.id.insert_edit_name); ageView = (EditText) findViewById(R.id.insert_edit_age); sexView = (EditText) findViewById(R.id.insert_edit_sex); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.insert_confirm: confirm(); // 確認插入 break; case R.id.insert_cancel: cancel(); // 取消插入 break; } } /** * 確認插入 */ private void confirm() { String name = String.valueOf(nameView.getText()); int age = Integer.valueOf(String.valueOf(ageView.getText())); String sex = String.valueOf(sexView.getText()); Person person = new Person(); person.setName(name); person.setAge(age); person.setSex(sex); Intent intent = new Intent(); intent.putExtra("person", person); setResult(INSERT_REQUESTCODE, intent); finish(); } /** * 取消插入 */ private void cancel() { setResult(INSERT_REQUESTCODE); finish(); } }
package com.iwanghang.sqlitedemo.dialog; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.EditText; import com.iwanghang.sqlitedemo.Person; import com.iwanghang.sqlitedemo.R; /** * 修改對話方塊 */ public class UpdateDialog extends Activity implements View.OnClickListener { /** * 修改請求程式碼 */ private static final int UPDATE_REQUESTCODE = 3; /** * id控制元件 */ private EditText idView; /** * 姓名控制元件 */ private EditText nameView; /** * 年齡控制元件 */ private EditText ageView; /** * 性別控制元件 */ private EditText sexView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉標題欄 setContentView(R.layout.update_dialog); init(); } private void init() { findViewById(R.id.update_confirm).setOnClickListener(this); findViewById(R.id.update_cancel).setOnClickListener(this); idView = (EditText) findViewById(R.id.update_edit_id); nameView = (EditText) findViewById(R.id.update_edit_name); ageView = (EditText) findViewById(R.id.update_edit_age); sexView = (EditText) findViewById(R.id.update_edit_sex); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.update_confirm: confirm(); // 確認修改 break; case R.id.update_cancel: cancel(); // 取消修改 break; } } /** * 確認修改 */ private void confirm() { int _id = Integer.valueOf(String.valueOf(idView.getText())); String name = String.valueOf(nameView.getText()); int age = Integer.valueOf(String.valueOf(ageView.getText())); String sex = String.valueOf(sexView.getText()); Person person = new Person(); person.set_id(_id); person.setAge(age); person.setName(name); person.setSex(sex); Intent intent = new Intent(); intent.putExtra("person", person); setResult(UPDATE_REQUESTCODE, intent); finish(); } /** * 取消修改 */ private void cancel() { setResult(UPDATE_REQUESTCODE); finish(); }
package com.iwanghang.sqlitedemo.dialog; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.EditText; import com.iwanghang.sqlitedemo.R; /** * 刪除對話方塊 */ public class DeleteDialog extends Activity implements View.OnClickListener { /** * 插入請求程式碼 */ private static final int DELETE_REQUESTCODE = 2; /** * id控制元件 */ private EditText idView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉標題欄 setContentView(R.layout.delete_dialog); init(); } private void init() { findViewById(R.id.delete_confirm).setOnClickListener(this); findViewById(R.id.delete_cancel).setOnClickListener(this); idView = (EditText) findViewById(R.id.delete_edit_id); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.delete_confirm: confirm(); // 確認刪除 break; case R.id.delete_cancel: cancel(); // 取消刪除 break; } } /** * 確認刪除 */ private void confirm() { int _id = Integer.valueOf(String.valueOf(idView.getText())); Intent intent = new Intent(); intent.putExtra("_id", _id); setResult(DELETE_REQUESTCODE, intent); finish(); } /** * 取消刪除 */ private void cancel() { setResult(DELETE_REQUESTCODE); finish(); } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <!-- 增加 --> <Button android:id="@+id/btn_insert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/btn_insert" /> <!-- 刪除 --> <Button android:id="@+id/btn_delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/btn_insert" android:text="@string/btn_delete" /> <!-- 修改 --> <Button android:id="@+id/btn_update" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/btn_delete" android:text="@string/btn_update" /> <!-- 查詢 --> <Button android:id="@+id/btn_query" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/btn_update" android:text="@string/btn_query" /> <!-- 展現查詢的資料--> <ListView android:id="@+id/list_data" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/btn_query" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 顯示Person欄位 _id --> <TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="_id" android:textSize="20dp" /> <!-- 顯示Person欄位 name --> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:textSize="20dp" android:layout_marginLeft="70dp" android:layout_marginStart="70dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/text_id" android:layout_toEndOf="@+id/text_id" /> <!-- 顯示Person欄位 age --> <TextView android:id="@+id/text_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age" android:textSize="20dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/text_name" android:layout_toEndOf="@+id/text_name" android:layout_marginLeft="70dp" android:layout_marginStart="70dp" /> <!-- 顯示Person欄位 sex --> <TextView android:id="@+id/text_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sex" android:textSize="20dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="40dp" android:layout_marginEnd="40dp" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <!-- 插入對話方塊線性佈局對於姓名欄位 --> <LinearLayout android:id="@+id/insert_linearLayout_name" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 姓名提示 --> <TextView android:id="@+id/insert_text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert_text_name" android:textSize="20dp" /> <!-- 姓名輸入 --> <EditText android:id="@+id/insert_edit_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- 插入對話方塊線性佈局對於年齡欄位 --> <LinearLayout android:id="@+id/insert_linearLayout_age" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/insert_linearLayout_name" > <!-- 年齡提示 --> <TextView android:id="@+id/insert_text_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert_text_age" android:textSize="20dp" /> <!-- 年齡輸入 --> <EditText android:id="@+id/insert_edit_age" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/insert_linearLayout_age" android:layout_centerHorizontal="true"> <!-- 性別提示 --> <TextView android:id="@+id/insert_text_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert_text_sex" android:textSize="20dp" /> <!-- 性別輸入 --> <EditText android:id="@+id/insert_edit_sex" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- 確認插入按鈕 --> <Button android:id="@+id/insert_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert_confirm" android:layout_below="@+id/linearLayout" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <!-- 取消插入按鈕 --> <Button android:id="@+id/insert_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert_cancel" android:layout_below="@+id/linearLayout" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>