查看: 486|回复: 0

[手机开发] Android 中SQLite技术实例详解

发表于 2017-11-27 08:00:06

Android和iOS的数据库都是用SQLite来实现.

一,SQLite数据库简介:

轻量级:SQLite数据库是一个轻量级的数据库,适用于少量数据的CURD;

文件本质:SQLite数据库支持大部分SQL语法,允许使用SQL语句操作数据库,其本质是一个文件,不需要安装启动。

数据读写:SQLite数据库打开只是一个文件的读写流。

二.简单的数据库语句知识

在android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持NULL,INTEGER,REAL(浮点数字),TEXT(字符串文本)和BLOB(二进制对象)数据类型,实际上sqlite3也接受varchar(n),char(n),decimal(p,s)等数据类型,只不过在运算或保存时会转成对应的五种数据类型.

SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么。

今天我就用一个简单的列子来说明来实现SQLite。

实例代码:

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import com.hucc.huccgps.R;
  4. import android.app.Activity;
  5. import android.app.Dialog;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.view.ViewGroup;
  16. import android.view.Window;
  17. import android.widget.AdapterView;
  18. import android.widget.AdapterView.OnItemClickListener;
  19. import android.widget.BaseAdapter;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.ImageView;
  23. import android.widget.ListView;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. public class MainActivity extends Activity {
  27. private static final String TAG = "JGPS/MainActivity";
  28. private TextView mAdd_white = null;
  29. private ListView mWhiteListView = null;
  30. private Context mContext = null;
  31. private int mPosition;
  32. private DBHelper mDBHelper = null;
  33. private WhiteListAdapter mAdapter = null;
  34. private ArrayList<Item> mWhiteListItem = new ArrayList<Item>();
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. Log.d("JGPS/Start", "onCreate");
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.relative_main);
  40. this.mDBHelper = new DBHelper(this);
  41. this.mContext = this;
  42. this.mWhiteListItem = new ArrayList<Item>();
  43. this.mWhiteListView = (ListView)findViewById(R.id.list_view);
  44. initData();
  45. this.mWhiteListView.setOnItemClickListener(new OnItemClickListener() {
  46. @Override
  47. public void onItemClick(AdapterView<?> paramAdapterView, View paramView,
  48. int paramInt, long paramLong) {
  49. Log.d("JGPS/MainActivity", ""+paramInt);
  50. MainActivity.this.updateWhiteList((Item)MainActivity.this.mWhiteListItem.get(paramInt));
  51. }
  52. });
  53. if (this.mAdd_white == null)
  54. return;
  55. mAdd_white.setOnClickListener(new OnClickListener() {
  56. @Override
  57. public void onClick(View arg0) {
  58. MainActivity.this.addContactList();
  59. }
  60. });
  61. Log.d("JGPS/end", "onCreate");
  62. }
  63. private void initData() {
  64. this.mWhiteListItem = this.mDBHelper.getRelativesList();
  65. if (this.mWhiteListItem.size() != 3) {
  66. for (int i = 1; i < 4; i++) {
  67. Log.d("JGPS/MainActivity", "initData,id=" + i);
  68. Item localItem1 = new Item();
  69. localItem1.setName("");
  70. localItem1.setNumber("");
  71. addItemToList(localItem1);
  72. updateSetting(i,"");
  73. }
  74. this.mWhiteListItem = this.mDBHelper.getRelativesList();
  75. }
  76. Iterator localIterator = this.mWhiteListItem.iterator();
  77. while (localIterator.hasNext()) {
  78. Item localItem2 = (Item) localIterator.next();
  79. Log.d("JGPS/MainActivity", "initData,id=" + localItem2.getId() + ",name = " + localItem2.getName() + ",number=" + localItem2.getNumber());
  80. }
  81. this.mAdapter = new WhiteListAdapter(this, this.mWhiteListItem);
  82. this.mWhiteListView.setAdapter(mAdapter);
  83. }
  84. private void updateSetting(int id, String phoneNum) {
  85. switch (id) {
  86. case 1:
  87. putSting("phone_num1",phoneNum);
  88. break;
  89. case 2:
  90. putSting("phone_num2",phoneNum);
  91. break;
  92. case 3:
  93. putSting("phone_num3",phoneNum);
  94. break;
  95. default:
  96. Log.d("JGPS/MainActivity", "Wrong relative number id");
  97. }
  98. }
  99. private void putSting(String paramString1, String paramString2) {
  100. SharedPreferences.Editor localEditor = getSharedPreferences("sos_config", 0).edit();
  101. localEditor.putString(paramString1, paramString2);
  102. localEditor.apply();
  103. }
  104. protected void updateWhiteList(Item paramItem) {
  105. final Item item = paramItem;
  106. Log.d("JGPS/MainActivity", "updateWhiteList------" + item.getId());
  107. final Dialog localDialog = new Dialog(this, R.style.dialog);
  108. localDialog.show();
  109. Window localWindow = localDialog.getWindow();
  110. localWindow.setContentView(R.layout.dialog_editor);
  111. Button localButton1 = (Button) localWindow.findViewById(R.id.btn_ok);
  112. Button localButton2 = (Button) localWindow.findViewById(R.id.btn_cancel);
  113. final EditText phone = (EditText)localWindow.findViewById(R.id.phone);
  114. final EditText name = (EditText)localWindow.findViewById(R.id.name);
  115. name.setText(item.getName());
  116. phone.setText(item.getNumber());
  117. ((TextView)localWindow.findViewById(R.id.title)).setText(R.string.edit_white);
  118. localButton1.setOnClickListener(new OnClickListener() {
  119. @Override
  120. public void onClick(View paramView) {
  121. if (phone.getText().toString().trim().length() == 0) {
  122. Toast.makeText(mContext, R.string.toast_tel, 2000).show();
  123. return;
  124. }
  125. if (name.getText().toString().trim().length() == 0) {
  126. Toast.makeText(mContext, R.string.name_null, 2000).show();
  127. return;
  128. }
  129. MainActivity.this.updateContactInDB(item.getId(), name.getText().toString(), phone.getText().toString());
  130. int id = Integer.parseInt(item.getId());
  131. updateSetting(id, phone.getText().toString());
  132. Toast.makeText(mContext, R.string.edit_success, 2000).show();
  133. localDialog.cancel();
  134. }
  135. });
  136. localButton2.setOnClickListener(new OnClickListener() {
  137. @Override
  138. public void onClick(View paramView) {
  139. localDialog.cancel();
  140. }
  141. });
  142. }
  143. protected void updateContactInDB(String paramString1, String paramString2, String paramString3) {
  144. Item localItem = new Item();
  145. localItem.setId(paramString1);
  146. localItem.setName(paramString2);
  147. localItem.setNumber(paramString3);
  148. updateItemList(localItem);
  149. }
  150. private void updateItemList(Item paramItem) {
  151. Item localItem2;
  152. Iterator localIterator1 = this.mWhiteListItem.iterator();
  153. while (localIterator1.hasNext()) {
  154. localItem2 = (Item) localIterator1.next();
  155. if (localItem2.getId().equals(paramItem.getId())) {
  156. localItem2.setName(paramItem.getName());
  157. localItem2.setNumber(paramItem.getNumber());
  158. this.mDBHelper.updateRelativesItem(paramItem);
  159. }
  160. }
  161. Log.d("JGPS/MainActivity", "updateItemInList ---------");
  162. this.mAdapter = new WhiteListAdapter(this, this.mWhiteListItem);
  163. this.mWhiteListView.setAdapter(mAdapter);
  164. }
  165. private void addContactList() {
  166. final Dialog dialog = new Dialog(this, R.style.dialog);
  167. dialog.show();
  168. Window window = dialog.getWindow();
  169. window.setContentView(R.layout.dialog_editor);
  170. Button btn1 = (Button)window.findViewById(R.id.btn_ok);
  171. Button btn2 = (Button)window.findViewById(R.id.btn_cancel);
  172. final EditText phone = (EditText)window.findViewById(R.id.phone);
  173. final EditText name = (EditText)window.findViewById(R.id.name);
  174. btn1.setOnClickListener(new OnClickListener() {
  175. @Override
  176. public void onClick(View arg0) {
  177. if (phone.getText().toString().trim().length() == 0)
  178. {
  179. Toast.makeText(mContext, R.string.toast_tel, 2000).show();
  180. return;
  181. }
  182. if (name.getText().toString().trim().length() == 0)
  183. {
  184. Toast.makeText(mContext, R.string.name_null, 2000).show();
  185. return;
  186. }
  187. if (MainActivity.this.mWhiteListItem.size()>3) {
  188. String str = MainActivity.this.getResources().getString(R.string.full1)+ " " + 3 + " " + MainActivity.this.getResources().getString(R.string.full2);
  189. Toast.makeText(mContext, str, 2000).show();
  190. dialog.cancel();
  191. return;
  192. }
  193. MainActivity.this.addContactToDB(name.getText().toString(),phone.getText().toString());
  194. dialog.cancel();
  195. }
  196. });
  197. btn2.setOnClickListener(new OnClickListener() {
  198. @Override
  199. public void onClick(View arg0) {
  200. dialog.cancel();
  201. }
  202. });
  203. }
  204. protected void deleteContactFromDB(String paramString) {
  205. updateContactInDB(paramString, "", "");
  206. Log.d("JGPS/MainActivity", "===addItemToList = "+paramString);
  207. int id = Integer.parseInt(paramString);
  208. updateSetting(id, paramString);
  209. Toast.makeText(this.mContext, R.string.delete_success, 2000).show();
  210. }
  211. @Override
  212. protected void onStart() {
  213. Log.d("JGPS/MainActivity", "onStart()");
  214. super.onStart();
  215. }
  216. protected void startContactIntent(int paramInt) {
  217. Log.d("JGPS/MainActivity", "onClick " + paramInt);
  218. this.mPosition = paramInt;
  219. Intent localIntent = new Intent("android.intent.action.PICK");
  220. localIntent.setType("vnd.android.cursor.dir/phone_v2");
  221. startActivityForResult(localIntent, 100);
  222. }
  223. protected void addContactToDB(String paramString1, String paramString2) {
  224. Iterator localIterator = this.mWhiteListItem.iterator();
  225. while (localIterator.hasNext()) {
  226. if(((Item)localIterator.next()).getNumber().equals(paramString2)){
  227. Toast.makeText(mContext, R.string.replace, 2000).show();
  228. return;
  229. }
  230. }
  231. Item localItem = new Item();
  232. localItem.setName(paramString1);
  233. localItem.setNumber(paramString2);
  234. addItemToList(localItem);
  235. Toast.makeText(mContext, R.string.replace, 2000).show();
  236. }
  237. private void addItemToList(Item localItem) {
  238. Log.d("JGPS/MainActivity", "addItemToList,name = " + localItem.getName() + ",number=" + localItem.getNumber());
  239. this.mDBHelper.addRelativesItem(localItem);
  240. this.mWhiteListItem = this.mDBHelper.getRelativesList();
  241. this.mAdapter = new WhiteListAdapter(this, this.mWhiteListItem);
  242. this.mWhiteListView.setAdapter(mAdapter);
  243. }
  244. public void startActivityForResult(Intent paramIntent, int paramInt)
  245. {
  246. super.startActivityForResult(paramIntent, paramInt);
  247. }
  248. private class WhiteListAdapter extends BaseAdapter{
  249. private Context context;
  250. private LayoutInflater mInflater;
  251. private ArrayList<Item> listItem;
  252. public WhiteListAdapter(Context context1,
  253. ArrayList<Item> whiteListItem) {
  254. this.context = context1;
  255. this.listItem = whiteListItem;
  256. this.mInflater = LayoutInflater.from(context);
  257. }
  258. public int getCount()
  259. {
  260. if ((this.listItem !=null) && (this.listItem.size() > 0)) {
  261. return 3;
  262. }
  263. return 0;
  264. }
  265. public Object getItem(int paramInt)
  266. {
  267. if ((this.listItem != null) && (this.listItem.size() > 0))
  268. return this.listItem.get(paramInt);
  269. return null;
  270. }
  271. public long getItemId(int paramInt)
  272. {
  273. return 0L;
  274. }
  275. @Override
  276. public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
  277. Log.d("JGPS/MainActivity", "getView,position=" + paramInt + ",name=" + ((Item)this.listItem.get(paramInt)).getName() + ",num=" + ((Item)this.listItem.get(paramInt)).getNumber());
  278. if (paramView == null) {
  279. final ViewHolder localViewHolder = new ViewHolder();
  280. paramView = this.mInflater.inflate(R.layout.relative_list_item, null);
  281. localViewHolder.ivCall = (ImageView)paramView.findViewById(R.id.imageView_call);
  282. localViewHolder.ivDelete = (ImageView)paramView.findViewById(R.id.imageView_delete);
  283. localViewHolder.ivSelect = (ImageView)paramView.findViewById(R.id.recipients_picker);
  284. localViewHolder.txtView_name = (TextView)paramView.findViewById(R.id.txtView_name);
  285. localViewHolder.txtView_tel = (TextView)paramView.findViewById(R.id.txtView_number);
  286. paramView.setTag(localViewHolder);
  287. localViewHolder.position = paramInt;
  288. localViewHolder.ivSelect.setTag(localViewHolder);
  289. if ((this.listItem.get(paramInt)== null)||
  290. (((Item)this.listItem.get(paramInt)).getNumber() == null)
  291. || (((Item)this.listItem.get(paramInt)).getNumber().equals(""))) {
  292. localViewHolder.ivCall.setVisibility(View.GONE);
  293. localViewHolder.ivSelect.setVisibility(View.VISIBLE);
  294. String str1 = MainActivity.this.getString(R.string.family_string);
  295. String str2 = str1 + (paramInt + 1) + ":";
  296. localViewHolder.txtView_name.setText(str2);
  297. localViewHolder.txtView_tel.setText(R.string.click_to_add);
  298. }else {
  299. localViewHolder.ivCall.setVisibility(View.VISIBLE);
  300. localViewHolder.ivSelect.setVisibility(View.GONE);
  301. localViewHolder.txtView_name.setText(((Item)this.listItem.get(paramInt)).getName());
  302. localViewHolder.txtView_tel.setText(((Item)this.listItem.get(paramInt)).getNumber());
  303. }
  304. localViewHolder.ivSelect.setOnClickListener(new OnClickListener() {
  305. @Override
  306. public void onClick(View paramView) {
  307. MainActivity.WhiteListAdapter.ViewHolder localViewHolder = (MainActivity.WhiteListAdapter.ViewHolder)paramView.getTag();
  308. Log.d("JGPS/MainActivity", "getView,mPosition=" + MainActivity.this.mPosition);
  309. MainActivity.this.startContactIntent(localViewHolder.position);
  310. }
  311. });
  312. if ((!(((Item)this.listItem.get(paramInt)).getName().equals("")))
  313. || (!(((Item)this.listItem.get(paramInt)).getNumber().equals(""))))
  314. {
  315. localViewHolder.ivDelete.setVisibility(View.VISIBLE);
  316. }else {
  317. localViewHolder.ivDelete.setVisibility(View.GONE);
  318. }
  319. localViewHolder.ivCall.setOnClickListener(new OnClickListener() {
  320. @Override
  321. public void onClick(View paramView) {
  322. Intent localIntent = new Intent("android.intent.action.CALL",
  323. Uri.parse("tel:" + ((Item)MainActivity.WhiteListAdapter.this.listItem.get(localViewHolder.position)).getNumber()));
  324. MainActivity.WhiteListAdapter.this.context.startActivity(localIntent);
  325. }
  326. });
  327. localViewHolder.ivDelete.setOnClickListener(new OnClickListener() {
  328. @Override
  329. public void onClick(View paramView) {
  330. if(paramView.getId() != R.id.imageView_delete)
  331. return;
  332. final String str = ((Item)MainActivity.WhiteListAdapter.this.listItem.get(localViewHolder.position)).getId();
  333. Log.d("JGPS/MainActivity", "delete item onClick,strId=" + str + ",position=" + localViewHolder.position);
  334. final Dialog localDialog = new Dialog(MainActivity.WhiteListAdapter.this.context, R.style.dialog);
  335. localDialog.show();
  336. Window localWindow = localDialog.getWindow();
  337. localWindow.setContentView(R.layout.dialog_delete);
  338. ((TextView)localWindow.findViewById(R.id.txtView_delete)).setText(MainActivity.WhiteListAdapter.this.context.getResources().getString(R.string.delete_one)
  339. + "'" + ((Item)MainActivity.WhiteListAdapter.this.listItem.get(localViewHolder.position)).getName()
  340. + "'" + MainActivity.WhiteListAdapter.this.context.getResources().getString(R.string.delete_two));
  341. Button localButton1 = (Button) localWindow.findViewById(R.id.btn_ok);
  342. Button localButton2 = (Button) localWindow.findViewById(R.id.btn_cancel);
  343. localButton1.setOnClickListener(new OnClickListener() {
  344. @Override
  345. public void onClick(View paramView) {
  346. MainActivity.this.deleteContactFromDB(str);
  347. localDialog.cancel();
  348. }
  349. });
  350. localButton2.setOnClickListener(new OnClickListener() {
  351. @Override
  352. public void onClick(View paramView) {
  353. localDialog.cancel();
  354. }
  355. });
  356. }
  357. });
  358. }else {
  359. }
  360. return paramView;
  361. }
  362. public class ViewHolder{
  363. public ImageView ivCall;
  364. public ImageView ivDelete;
  365. public ImageView ivSelect;
  366. public int position;
  367. public TextView txtView_name;
  368. public TextView txtView_tel;
  369. }
  370. }
  371. }
  372. import java.util.ArrayList;
  373. import android.content.ContentValues;
  374. import android.content.Context;
  375. import android.database.Cursor;
  376. import android.database.sqlite.SQLiteDatabase;
  377. import android.database.sqlite.SQLiteOpenHelper;
  378. import android.util.Log;
  379. public class DBHelper extends SQLiteOpenHelper {
  380. public static final String TABLE_RELATIVES_CONTENT = "RelativesContent";
  381. public static final int VERSION = 1; //版本
  382. public static final String dbName = "SosDB"; //数据库的名字
  383. public static Context mContext = null;
  384. public SQLiteDatabase db = null;
  385. public DBHelper(Context paramContext) {
  386. super(paramContext, "SosDB", null, 1);
  387. mContext = paramContext;
  388. }
  389. // 创建数据库表
  390. @Override
  391. public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
  392. paramSQLiteDatabase.execSQL("create table RelativesContent(id integer primary key autoincrement,name text , number text)");
  393. closeDatabase();
  394. }
  395. private void closeDatabase() {
  396. if (this.db == null)
  397. return;
  398. this.db.close();
  399. }
  400. @Override
  401. public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2) {
  402. onCreate(paramSQLiteDatabase);
  403. }
  404. //查询方法
  405. public ArrayList<Item> getRelativesList() {
  406. String idf = null;
  407. String namef = null;
  408. String numf = null;
  409. ArrayList localArrayList = new ArrayList();
  410. openDatabase();
  411. Cursor localCursor = this.db.query("RelativesContent", null, null, null, null, null, null);
  412. if(localCursor == null)
  413. return localArrayList;
  414. while (localCursor.moveToNext()) {
  415. Item localItem = new Item();
  416. idf = localCursor.getString(localCursor.getColumnIndex("id"));
  417. namef = localCursor.getString(localCursor.getColumnIndex("name"));
  418. numf = localCursor.getString(localCursor.getColumnIndex("number"));
  419. localItem.setId(idf);
  420. Log.d("JGPS/MainActivity", "dbid=" + idf);
  421. if ((namef == null) || ("".equals(namef))) {
  422. localItem.setName("");
  423. }else {
  424. localItem.setName(namef);
  425. }
  426. if ((numf == null) || ("".equals(numf))) {
  427. localItem.setNumber("");
  428. }else {
  429. localItem.setNumber(numf);
  430. }
  431. localArrayList.add(localItem);
  432. }
  433. return localArrayList;
  434. }
  435. //写入数据
  436. private void openDatabase() {
  437. this.db = super.getWritableDatabase();
  438. }
  439. //添加数据
  440. public boolean addRelativesItem(Item localItem) {
  441. openDatabase();
  442. ContentValues localContentValues = new ContentValues();
  443. localContentValues.put("name", localItem.getName());
  444. localContentValues.put("number", localItem.getNumber());
  445. long l = this.db.insert("RelativesContent", null, localContentValues);
  446. closeDatabase();
  447. return (1<=0L);
  448. }
  449. //更新数据
  450. public boolean updateRelativesItem(Item paramItem) {
  451. openDatabase();
  452. ContentValues localContentValues = new ContentValues();
  453. localContentValues.put("name", paramItem.getName());
  454. localContentValues.put("number", paramItem.getNumber());
  455. SQLiteDatabase localSQLiteDatabase = this.db;
  456. String[] arrayOfString = new String[1];
  457. arrayOfString[0] = paramItem.getId();
  458. long l = localSQLiteDatabase.update("RelativesContent", localContentValues, "id=?", arrayOfString);
  459. closeDatabase();
  460. return (1 <= 0L);
  461. }
  462. }
  463. import java.io.Serializable;
  464. public class Item implements Serializable{
  465. private String _id;
  466. private String name;
  467. private String number;
  468. public Item() {
  469. // TODO Auto-generated constructor stub
  470. }
  471. public void Item(String paramString1, String paramString2, String paramString3)
  472. {
  473. this._id = paramString1;
  474. this.name = paramString2;
  475. this.number = paramString3;
  476. }
  477. public String getId() {
  478. return _id;
  479. }
  480. public void setId(String paramString)
  481. {
  482. this._id = paramString;
  483. }
  484. public String getName() {
  485. return name;
  486. }
  487. public void setName(String paramString)
  488. {
  489. this.name = paramString;
  490. }
  491. public String getNumber() {
  492. return number;
  493. }
  494. public void setNumber(String paramString)
  495. {
  496. this.number = paramString;
  497. }
  498. }
复制代码

这是基本的数据程序。

XML

relative_main

  1. <LinearLayout android:orientation="vertical"
  2. android:background="@color/background_color"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. xmlns:android="http://schemas.android.com/apk/res/android">
  6. <RelativeLayout android:background="@drawable/title_bar"
  7. android:layout_width="fill_parent"
  8. android:layout_height="50.0sp">
  9. <TextView
  10. android:textSize="@dimen/title_size"
  11. android:textStyle="bold"
  12. android:textColor="@color/title_color"
  13. android:gravity="center"
  14. android:layout_gravity="center"
  15. android:layout_width="wrap_content"
  16. android:layout_height="50.0sp"
  17. android:text="@string/relative_name"
  18. android:layout_centerInParent="true" />
  19. </RelativeLayout>
  20. <RelativeLayout android:id="@+id/white_content"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content">
  23. <ListView android:id="@+id/list_view"
  24. android:scrollbars="none"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:scrollingCache="true"
  28. android:cacheColorHint="#00000000"
  29. android:divider="@null"
  30. android:fastScrollEnabled="false" />
  31. </RelativeLayout>
  32. </LinearLayout>
  33. dialog_editor
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout
  36. xmlns:android="http://schemas.android.com/apk/res/android"
  37. android:gravity="center"
  38. android:orientation="vertical"
  39. android:id="@+id/linearlayout_dialog"
  40. android:layout_width="300.0dip"
  41. android:layout_height="wrap_content">
  42. <TextView
  43. android:textSize="20.0sp"
  44. android:textColor="#ffffffff"
  45. android:gravity="center"
  46. android:id="@+id/title"
  47. android:background="@drawable/popup_topbg"
  48. android:layout_width="fill_parent"
  49. android:layout_height="60.0dip"
  50. android:text="@string/add_white" />
  51. <LinearLayout
  52. android:orientation="vertical"
  53. android:background="@drawable/simple_dialog_list_item_bg"
  54. android:paddingLeft="5.0dip" android:paddingTop="5.0dip"
  55. android:paddingRight="5.0dip" android:layout_width="fill_parent"
  56. android:layout_height="wrap_content">
  57. <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content"
  58. android:layout_height="wrap_content" android:layout_marginLeft="8.0dip"
  59. android:layout_marginTop="6.0dip" android:layout_marginRight="6.0dip"
  60. android:layout_marginBottom="2.0dip">
  61. <TextView android:textSize="16.0sp" android:textColor="#ff000000"
  62. android:layout_width="wrap_content" android:layout_height="wrap_content"
  63. android:text="@string/name" />
  64. <EditText android:gravity="left|center"
  65. android:id="@+id/name"
  66. android:background="@drawable/text_input"
  67. android:layout_width="210.0dip" android:layout_height="wrap_content"
  68. android:layout_marginLeft="8.0dip" android:hint="@string/name_hint"
  69. android:singleLine="true" />
  70. </LinearLayout>
  71. <LinearLayout android:layout_gravity="center" android:orientation="horizontal"
  72. android:layout_width="fill_parent" android:layout_height="wrap_content"
  73. android:layout_marginLeft="8.0dip" android:layout_marginRight="6.0dip">
  74. <TextView android:textSize="16.0sp" android:textColor="#ff000000"
  75. android:layout_width="wrap_content" android:layout_height="wrap_content"
  76. android:text="@string/address" />
  77. <EditText android:gravity="left|center" android:id="@+id/phone"
  78. android:background="@drawable/text_input"
  79. android:layout_width="210.0dip"
  80. android:layout_height="wrap_content" android:layout_marginLeft="8.0dip"
  81. android:hint="@string/address_hint" android:singleLine="true"
  82. android:inputType="phone" />
  83. </LinearLayout>
  84. <include layout="@layout/divider" />
  85. <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
  86. android:layout_height="54.0dip">
  87. <Button android:textSize="20.0sp" android:id="@+id/btn_ok"
  88. android:layout_width="fill_parent"
  89. android:layout_height="fill_parent" android:text="@string/ok"
  90. android:layout_weight="5.0" style="@style/dialog_item" />
  91. <View android:background="#ffbcc2c5" android:layout_width="2.0px"
  92. android:layout_height="fill_parent" />
  93. <Button android:textSize="20.0sp" android:id="@+id/btn_cancel"
  94. android:layout_width="fill_parent"
  95. android:layout_height="fill_parent"
  96. android:text="@string/cancel" android:layout_weight="5.0"
  97. style="@style/dialog_item" />
  98. </LinearLayout>
  99. </LinearLayout>
  100. </LinearLayout>
复制代码

relative_list_item

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:gravity="center_vertical"
  4. android:orientation="horizontal"
  5. android:layout_width="fill_parent"
  6. android:layout_height="40.0dip"
  7. style="@style/list_item_background">
  8. <LinearLayout
  9. android:gravity="center_vertical"
  10. android:orientation="horizontal"
  11. android:id="@+id/ll_line1"
  12. android:paddingLeft="5.0dip"
  13. android:paddingRight="5.0dip"
  14. android:layout_width="0.0dip"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1.0">
  17. <TextView
  18. android:textSize="@dimen/list_font_size_0"
  19. android:textStyle="bold"
  20. android:textColor="@color/list_item_text"
  21. android:ellipsize="end"
  22. android:gravity="center"
  23. android:id="@+id/txtView_name"
  24. android:paddingBottom="3.0dip"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:maxWidth="80.0dip"
  28. android:singleLine="true" />
  29. <TextView
  30. android:textSize="@dimen/list_font_size_1"
  31. android:textStyle="bold"
  32. android:textColor="@color/list_item_text"
  33. android:ellipsize="end" android:gravity="center"
  34. android:id="@+id/txtView_number" android:layout_width="wrap_content"
  35. android:layout_height="wrap_content" android:layout_marginLeft="10.0dip"
  36. android:singleLine="true" />
  37. </LinearLayout>
  38. <ImageView
  39. android:layout_gravity="center_vertical"
  40. android:id="@+id/imageView_call"
  41. android:background="@drawable/btn_dial_action"
  42. android:paddingLeft="8.0dip"
  43. android:paddingTop="8.0dip"
  44. android:paddingRight="8.0dip"
  45. android:paddingBottom="8.0dip"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:src="@drawable/call_phone" />"
  49. <ImageView android:layout_gravity="center_vertical"
  50. android:id="@+id/recipients_picker"
  51. android:background="@drawable/add_contact_selector"
  52. android:layout_width="65.0dip" android:layout_height="65.0dip"
  53. android:layout_marginLeft="2.0dip" android:layout_marginRight="2.0dip"
  54. android:scaleType="fitXY" />
  55. <View android:id="@+id/imageView_divider" android:background="#2b2b2b2b"
  56. android:layout_width="2.0dip" android:layout_height="40.0dip" />
  57. <ImageView android:layout_gravity="right|center"
  58. android:id="@+id/imageView_delete" android:paddingLeft="8.0dip"
  59. android:paddingTop="8.0dip" android:paddingRight="8.0dip"
  60. android:paddingBottom="8.0dip" android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:src="@drawable/del_cross" />
  63. </LinearLayout>
复制代码

dialog_delete

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:gravity="center"
  3. android:orientation="vertical"
  4. android:id="@+id/linearLayout_dialog"
  5. android:layout_width="300.0dip" android:layout_height="wrap_content">
  6. <TextView android:textSize="20.0sp" android:textColor="#ffffffff"
  7. android:gravity="center" android:background="@drawable/popup_topbg"
  8. android:layout_width="fill_parent" android:layout_height="60.0dip"
  9. android:text="@string/tips" />
  10. <LinearLayout android:orientation="vertical"
  11. android:background="@drawable/simple_dialog_list_item_bg"
  12. android:paddingLeft="5.0dip"
  13. android:paddingTop="12.0dip" android:paddingRight="5.0dip"
  14. android:layout_width="fill_parent" android:layout_height="wrap_content">
  15. <TextView android:textSize="20.0sp" android:textColor="#ff000000"
  16. android:id="@+id/txtView_delete" android:paddingBottom="12.0dip"
  17. android:layout_width="wrap_content" android:layout_height="wrap_content"
  18. android:layout_marginLeft="8.0dip" android:layout_marginRight="6.0dip" />
  19. <include layout="@layout/divider" />
  20. <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
  21. android:layout_height="54.0dip">
  22. <Button android:textSize="20.0sp" android:id="@+id/btn_ok"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:text="@string/dialog_yes" android:layout_weight="5.0"
  26. style="@style/dialog_item" />
  27. <View android:background="#ffbcc2c5" android:layout_width="2.0px"
  28. android:layout_height="fill_parent" />
  29. <Button android:textSize="20.0sp" android:id="@+id/btn_cancel"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="@string/dialog_no" android:layout_weight="5.0"
  33. style="@style/dialog_item" />
  34. </LinearLayout>
  35. </LinearLayout>
  36. </LinearLayout>
复制代码

这是一个简单的demo,我这里就不详细说明数据库的细节了,对于已经很熟悉数据库的,可以查看android官网有说明的,

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条